This discussion is archived
5 Replies Latest reply: Apr 1, 2013 11:59 AM by jschellSomeoneStoleMyAlias RSS

Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7

1000139 Newbie
Currently Being Moderated
Hello Everybody...

I have a piece of software which establishes a communication between a PC and a machine, which used to work in Java 1.6, but doesn't anymore in 1.7. The IOException --> "Invalid Http response" is what I get as soon as the getInputStream() method is called. It seems like the method had been improved an is much more sensitive, meaning that responseCode=-1 result is not specifically checked in Java 1.6.

Assited with Wireshark I checked if both versions (1.6 & 1.7) sent and received same Ethernet frames, and so did both.

I can only resolve this from the PC (client) side...that means no posibility to edit or change the Server code.

I would apreciate any help on how to modify or implement something new to make the code compatible for ver. 1.7 as Im not a former programmer... Thanks

1. get is called
2. return readResponse(..) is called
3. getInputStream() --> IOException
4. catch (Exception e) {System.out.println("error sending get request " + getURL() + " string: " + requestString); return Error.ethernetException; //TODO


_*Code:*_

    private String get(String requestString)/* throws ComunicationException*/ {
        HttpURLConnection httpURLConnection = null;
        OutputStream out = null;       

        try {  
            String encodedRequestString = URLEncoder.encode(requestString, "iso-8859-1");
            String path = getURL().getPath();
            if (!path.endsWith("/")) path = path + "/";
            path = path + encodedRequestString;
            URL fullURL = new URL(getURL().getProtocol(), getURL().getHost(), getURL().getPort(), path);
            httpURLConnection = (HttpURLConnection)fullURL.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoOutput(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);

            // Set timeout at 5s
            httpURLConnection.setConnectTimeout(m_nTimeOut);
            httpURLConnection.setReadTimeout(m_nTimeOut);

            return readResponse(httpURLConnection);
           
        } catch (Exception e) {
          System.out.println("error sending get request " + getURL() + " string: " + requestString);
     return Error.ethernetException; //TODO
} finally {
if (out != null) {
try {
out.close();
} catch (Throwable t) {
     System.out.println("GET: out.close(), Class: Client");
}
}

if (httpURLConnection != null) {
try {
httpURLConnection.disconnect();
} catch (Throwable t) {
     System.out.println("GET: httpURLConnection.disconnect(), Class: Client");
}
}
}
}

/**
* Reads the response from the server into a string.
* The content length header must be set!
* @param connection
* @return
* @throws IOException
*/
private String readResponse(HttpURLConnection connection) throws IOException {
     if (connection == null) throw new IllegalStateException("connection must not be null");
     
connection.connect();
     
     int status = connection.getResponseCode();
     System.out.println(status);
     
     // InputStream aaa = connection.getInputStream();
     
     
     Reader reader = null;
     try {              
          reader = new InputStreamReader(connection.getInputStream(), "iso-8859-1");
     
          int readBufferSize = connection.getContentLength();
          if (readBufferSize < 0) {
               // use default buffer size
               readBufferSize = DEFAULT_READ_BUFFER_SIZE;
          }
          
          // if content length was set, read will be done in a single
          // iteration because buffer fits...
          StringBuffer response = new StringBuffer();
          char[] readBuffer = new char[readBufferSize];
          int len;
          while ((len = reader.read(readBuffer)) > 0) {
               response.append(new String(readBuffer, 0, len));                    
          }
          return response.toString();
               
          
                    
     } catch (IOException ioe) {
          throw ioe;      
     } finally {
          if (reader != null) {
               try {
                    reader.close();
               } catch (Throwable t) {
                    System.out.println("readResponse: reader.close(), Class: Client");
                    //log
               }
          }
     }
}

     /**
*
* @return the url
     */
     public URL getURL() {
          return url;
     }
}

Edited by: 997136 on 30-mar-2013 14:13
  • 1. Re: Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7
    EJP Guru
    Currently Being Moderated
    The Java class is designed to connect to an HTTP server.

    Any server that delivers a response code of -1 is not a valid HTTP server. Java is entitled to complain about this. You are going to have to fix the server. There is a large number of valid HTTP response codes. -1 is not one of them.
  • 2. Re: Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    I would apreciate any help on how to modify or implement something new to make the code compatible for ver. 1.7 as Im not a former programmer
    As noted it isn't a valid code. Just to verify I checked the spec for HTTP/1.1 and 1.0 to see if it was conceivable that it could have been valid and nothing I found suggests it was ever a valid value.

    So as noted the correct process is to fix the server.

    If you cannot then you would need to 'fix' the client code. The options there are
    1. Implement your own HTTP client code using a socket.
    2. Modify the Java API library that comes with the VM so it no longer treats that as an invalid code.

    My guess is that the second is easier in terms of code but is more of a kludge since you must modify the API and get the VM to consume it.

    If you fix the client code then document it extensively as to why you needed to fix it.

    If you need to demonstrate to someone that is in fact a bug then you can reference the following specification and note the part that says it is a +"*3-digit* integer result code"+ in the following.

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1
  • 3. Re: Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7
    1000139 Newbie
    Currently Being Moderated
    Status variable (int status = connection.getResponseCode()) is always -1 as seen by the console output either with Java 1.6 or 1.7, but with Java 1.6 the Client-Server communication works fine.


    console output with Java 1.7:
    -1
    error sending get request http://192.168.1.127:80/ string: *APPLETCOMMINFO_
    strResponse: Ethernet Exception


    console output with Java 1.6:
    -1
    nullsun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6f0ffb38
    strResponse: *APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
    default curser ether action: 4
    -1
    nullsun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2be2befa
    strResponse: *APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
    default curser ether action: 0
    -1
    nullsun.net.www.protocol.http.HttpURLConnection$HttpInputStream@7e859a68
    strResponse: *APPLETCOMMRDKP+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+3.50000+01+5.00000-01+3.50000+01+5.00000-01+3.50000+00+1.00000+02+1.00000+02+1.00000+02+1.00000+02+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+0000001000020000800192000000000000001000020000800192000000000000001000020000800192000000000000001000020000800192000000000000000000000000000000000000000100100100100200200200200300300300300000000000000100100100100000000000000000000000001301301301301401401401404905705604903203203203219216800112725525525500000000000000000100008000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111000000000000000000000000000000100200100210000000000000000001110000000000000000000000000000000000000000000000000000001111111111000111110000063805_
    default curser ether action: 0
    -1
    nullsun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1fe903d5
    strResponse: *APPLETCOMMRDPP+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+0.00000+00+0.00000+00+0.00000+00+0.00000+00+1.00000+02+1.00000+02+1.00000+02+1.00000+02+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000320320320320320320320320320320320320320320320320490570550550320320320320000127981_
    default curser ether action: 0
    true
  • 4. Re: Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7
    EJP Guru
    Currently Being Moderated
    In other words the problem you originally stated with the -1 response code in Java 1.7 is entirely imaginary.

    So what is your question?
  • 5. Re: Code with getInputStream() was compatible inJava 1.6, but anymore in 1.7
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Status variable (int status = connection.getResponseCode()) is always -1 as seen by the console output
    From the documentation for HttpURLConnection.getResponseCode()

    +"Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP). "+
    but with Java 1.6 the Client-Server communication works fine.
    I suggest that you get a tool that captures IP packets, run it on your system and see what the actual response is that you are getting back.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points