"无效的Http响应"在URLConnection.getInputStream()上仅适用于https url版本

时间:2017-05-23 08:33:58

标签: java https jetty

我在尝试访问简单的localhost https网址时收到一个非常奇怪的异常。

我的环境: 在Windows上运行jdk1.8.0_121,我从Netbeans运行java main,它打开了与配置了自签名证书的独立码头9.4.5中托管的https url的连接。

例外:

  

java.io.IOException:无效的Http响应   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在   sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1926)     在   sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1921)     在java.security.AccessController.doPrivileged(Native Method)at   sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920)     在   sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490)     在   sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)     在   sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

  • 相同的http网址工作正常
  • 在浏览器中访问https网址正常

在connection.openConnection()之后我尝试了:

int responseCode = ((HttpURLConnection) conn).getResponseCode();
String msg = ((HttpURLConnection) conn).getResponseMessage();

responseCode为-1且msg为空

HTTP响应标头:

Request URL:http://localhost:9393/MailProxyServer/index.jsp
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:9393
Referrer Policy:no-referrer-when-downgrade
Response Headers
view parsed
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Set-Cookie: JSESSIONID=node0113il7mdp4gwq1ff3b6gg32ks90.node0;Path=/MailProxyServer
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 328
Server: Jetty(9.4.5.v20170502)

HTTPS响应标头:

Request URL:https://localhost:8443/MailProxyServer/index.jsp
Request Method:GET
Status Code:200 
Remote Address:[::1]:8443
Referrer Policy:no-referrer-when-downgrade
Response Headers
content-type:text/html;charset=utf-8
server:Jetty(9.4.5.v20170502)
status:200

由于

1 个答案:

答案 0 :(得分:0)

最明显的原因是您的客户端和服务器之间有不同的ssl / tls / cipher / certificate规则。

由于您声明自己拥有自签名证书,因此您必须配置Java以了解该自签名证书。

将其添加到您启动java的密钥库中。 或者通过添加自定义证书处理来允许自签名证书。

有关详细信息,请参阅https://stackoverflow.com/a/2893932/775715