"Invalid Http response" 在 URLConnection.getInputStream() 上仅适用于 https url 版本



我在尝试访问一个简单的本地主机https url时收到一个非常奇怪的异常。

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

例外情况:

java.io.IOException:无效的 Http 响应 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 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) at 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();

响应代码为 -1,消息为空

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

谢谢

最明显的原因是客户端和服务器之间有不同的 ssl/tls/密码/证书规则。

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

通过将其添加到启动 Java 时使用的密钥库。 或者通过添加自定义证书处理来允许该自签名证书。

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

最新更新