0

I'm receiving a very strange exception trying to access a simple localhost https url.

My env: jdk1.8.0_121 on Windows, I'm running java main from Netbeans, it opens connection to https url hosted in a standalone jetty 9.4.5 configured with a self-signed cert.

The exception:

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)

  • The same http url works fine
  • Accessing the https url in browser works fine

After connection.openConnection() I tried:

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

responseCode is -1 and msg is null

HTTP Response Headers:

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 Reponse Headers:

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

Thanks

Gamby
  • 585
  • 1
  • 6
  • 22

1 Answers1

0

The most obvious reason is that you have different ssl/tls/cipher/certificate rules between your client and server.

Since you stated you have a self-signed cert, you'll have to configure Java to be aware of that self-signed cert.

Either by adding it to a keystore that you start java with. Or by adding custom certificate handling to allow that self-signed cert.

See https://stackoverflow.com/a/2893932/775715 for details

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • I've already configured Java to be aware of that self-signed cert, in fact before that I had "SunCertPathBuilderException: unable to find valid certification path to requested target". Please note that with the embedded jetty with maven it works fine – Gamby May 23 '17 at 14:28