httpclient 错误 笔记

No permitted “Access-Control-Allow-Origin” header.

net.sourceforge.htmlunit.corejs.javascript.WrappedException: Wrapped java.lang.RuntimeException: No permitted "Access-Control-Allow-Origin" header.
	at net.sourceforge.htmlunit.corejs.javascript.Context.throwAsScriptRuntimeEx(Context.java:2052) ~[htmlunit-core-js-2.23.jar:na]
	
Caused by: java.lang.RuntimeException: No permitted "Access-Control-Allow-Origin" header.
	... 9 common frames omitted

java.security.ProviderException: java.security.KeyException

Caused by: java.security.ProviderException: java.security.KeyException
  
Caused by: java.security.KeyException

我的JDK环境是sun的1.8.0,操作系统CentOS 6.5

运行如下linux命令后程序可以完美运行,BUG解决:yum upgrade nss

java.net.SocketException: Permission denied: connect

java.net.SocketException: Permission denied: connect
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_111]

被墙了

connect timed out

org.apache.http.conn.ConnectTimeoutException: Connect to zh.wikipedia.org:443 [zh.wikipedia.org/208.80.154.224] failed: connect timed out
	at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:132) ~[httpclient-4.3.5.jar:4.3.5]

Caused by: java.net.SocketTimeoutException: connect timed out
	

2个可能原因
1.被墙了
2.网断了

java.net.UnknownHostException

java.net.UnknownHostException: zh.wikipedia.org
  1. UnknownHost
  2. 可能被墙了

Content-Length header already present

org.apache.http.client.ClientProtocolException: null
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) ~[httpclient-4.5.2.jar:4.5.2]

Caused by: org.apache.http.ProtocolException: Content-Length header already present
	at org.apache.http.protocol.RequestContent.process(RequestContent.java:97) ~[httpcore-4.4.6.jar:4.4.6]

查看httpclient中RequestContent类的process方法,看到当body非空时,会自动加上Content-Length请求头及其对应值,不需要自己手动加上它。

参考 解决HttpClient:org.apache.http.ProtocolException异常

Read timed out

java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_111]

超时

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
	at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
	at sun.security.ssl.InputRecord.read(InputRecord.java:527)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
	at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
	at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:389)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
	at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)

遇到这个问题,可能有两种情况:

  1. 对方使用的https的链接,但其实用的是http的服务,遇到这种情况url用https,但是代理用http,然后就可以解决问题了。
  2. 对方服务器是HTTP Server,不是HTTPS Server,所以没必要用HTTPS,用HTTP就可以。虽然url显示的是https,但是你可以用http请求到。
  3. 强制jvm使用IPv4堆栈来解决错误 -Djava.net.preferIPv4Stack=true
  4. -Dhttps.protocols=”TLSv1”
  5. -Djsse.enableSNIExtension=false

I got the same error. it was because I was accessing the https port using http.. The issue solved when I changed http to https.

I think this is due to the connection established with the client machine is not secure.
It is due to the fact that you are talking to an HTTP server, not an HTTPS server. Probably you didn’t use the correct port number for HTTPS.

References

[1] http://www.javaranger.com/archives/674 解决HttpClient:org.apache.http.ProtocolException异常
[2] https://mp.weixin.qq.com/s/_Ya-tfBLukOAspg7L90gFA##
[3] http://www.data5u.com/api/create.html#javacode_2 创建API接口_接口文档_Java示例代码_无忧代理IP
[4] http://www.data5u.com/dayip-1-10.html 每日最新免费代理IP分享-无忧代理IP
[5] https://stackoverflow.com/questions/6532273/unrecognized-ssl-message-plaintext-connection-exception
[6] http://www.javaranger.com/archives/674