有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

URL连接上的java“无效Http响应”。getInputStream()仅适用于https url版本

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

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

例外情况:

java.io.IOException: Invalid Http response at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926) at 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) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

  • 相同的http url工作正常
  • 在浏览器中访问https url可以正常工作

连接后。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

谢谢


共 (1) 个答案

  1. # 1 楼答案

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

    因为您声明您有一个自签名证书,所以您必须配置Java以了解该自签名证书

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

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