Forum Stats

  • 3,784,045 Users
  • 2,254,876 Discussions
  • 7,880,656 Comments

Discussions

URLConnection.getInputStream() - "Invalid HttpResponse" Exception JRE-1.7.0

883419
883419 Member Posts: 4
edited Sep 7, 2011 9:13PM in Networking
The following code is throwing "java.io.IOException: Invalid Http response" when executed with few URLs in JRE 7.0

URL launchUrl = new URL("http://<URL>");
URLConnection uConn = launchUrl.openConnection();
uConn.getInputStream(); //The exception is thrown here

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at URLTest.main(URLTest.java:9)

The same code works fine when run from JRE 1.6.0_12.
The way java handles http request/response changed in JDK 7.0?
I the particular URL that is not working is present in GoAhead Webserver.

Best Answer

  • 880401
    880401 Member Posts: 10
    Accepted Answer
    Hello.

    My stack trace appears as follows for this issue.
    Exception in thread "main" java.io.IOException: Invalid Http response
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1328)
    	at test.UrlCon.main(UrlCon.java:30)
    The sun.net.www.protocol.http.HttpURLConnection class extends the java.net.HttpURLConnection class.
    In the java.net.HttpURLConnection method getResponse(), the return value is defined as -1 if the HTTP response code cannot be determined.

    In the Java 1.6 version of sun.net.www.protocol.http.HttpURLConnection class I have, this default/error response is not checked for specifically.
    Only protocol defined server response codes are looked at for special handling, such as the 200, 300 and 400 valid ranges.

    In the Java 1.7 version of getInputStream() for the sun.net.www.protocol.http.HttpURLConnection a significantly different implementation is present.
    And as soon as the call to java.net.HttpURLConnection getResponse() method returns, the result is checked immediately for the -1 value.
    In this instance the class is coded as
    throw new IOException("Invalid Http response");
    This is where you are getting it from, as this is the only place in the 1.7 deployed code base I could find with an IOException and anything like that exception message.

    In summary, it would appear that the response from your private URL is malformed?

    You might want to watch the client/server interaction with 'Wire Shark'?


    You can find the java source code for 1.6 versions here http://download.java.net/jdk6/source/

    Anyway, i hope this helps.

    Cheers

Answers

  • 880401
    880401 Member Posts: 10
    Hello.

    I tried your code on my machine, I added a little infrastructure around it, and it worked fine on both Java 6u6 and Java SE 7.
    I targeted several URLs.
    Perhaps if you posted the complete StackTrace some useful diagnostic information could be gleaned from it.

    Cheers.
  • DrClap
    DrClap Member Posts: 25,479
    And if those "few URLs" are public URLs, maybe you could post some of them too? It's possible that Java 7 included a fix which recognizes and rejects bad HTTP responses better; in other words the Java 6 version should have thrown exceptions but didn't.
  • 883419
    883419 Member Posts: 4
    The stack trace present in the query is complete.
  • 883419
    883419 Member Posts: 4
    The URL that is failing is a private URL. I will try with some public URLs, to identify the something which is not working. You may be correct, that Java 7 might have included few checks to reject bad requests/responses. I tried finding the source code of sun.net.www.protocol.http.HttpURLConnection.java file in JDK 7.0, to see if anything has changed, but in vain.
  • 880401
    880401 Member Posts: 10
    Accepted Answer
    Hello.

    My stack trace appears as follows for this issue.
    Exception in thread "main" java.io.IOException: Invalid Http response
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1328)
    	at test.UrlCon.main(UrlCon.java:30)
    The sun.net.www.protocol.http.HttpURLConnection class extends the java.net.HttpURLConnection class.
    In the java.net.HttpURLConnection method getResponse(), the return value is defined as -1 if the HTTP response code cannot be determined.

    In the Java 1.6 version of sun.net.www.protocol.http.HttpURLConnection class I have, this default/error response is not checked for specifically.
    Only protocol defined server response codes are looked at for special handling, such as the 200, 300 and 400 valid ranges.

    In the Java 1.7 version of getInputStream() for the sun.net.www.protocol.http.HttpURLConnection a significantly different implementation is present.
    And as soon as the call to java.net.HttpURLConnection getResponse() method returns, the result is checked immediately for the -1 value.
    In this instance the class is coded as
    throw new IOException("Invalid Http response");
    This is where you are getting it from, as this is the only place in the 1.7 deployed code base I could find with an IOException and anything like that exception message.

    In summary, it would appear that the response from your private URL is malformed?

    You might want to watch the client/server interaction with 'Wire Shark'?


    You can find the java source code for 1.6 versions here http://download.java.net/jdk6/source/

    Anyway, i hope this helps.

    Cheers
  • 883419
    883419 Member Posts: 4
    Hello,

    Thanks for the reply. Yes we were not sending the "HTTP/1.0" header as part of HTTP response from WebServer. Added the same and started working fine.
    Thanks for the help.
This discussion has been closed.