JAVA Networking: Code with getInputStream() was compatible with Java1.6, is but anymore with 1.7

Discussion in 'Java' started by musart, Mar 30, 2013.

  1. musart

    musart Guest

    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;
    }
    }
     
    musart, Mar 30, 2013
    #1
    1. Advertising

  2. Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    On 30/03/13 21:22, musart wrote:
    > 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.


    Get the menu on one of the HTTP-carrying frames, and select "Follow TCP
    Stream". What is the result? Does it look like a proper HTTP exchange?

    I would suggest calling getErrorStream() if you get an exception from
    getInputStream(), but if the response does not appear to be valid HTTP,
    I can't predict what getErrorStream() will actually give you.


    --
    ss at comp dot lancs dot ac dot uk
     
    Steven Simpson, Mar 31, 2013
    #2
    1. Advertising

  3. musart

    Daniel Pitts Guest

    Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    On 3/30/13 2:22 PM, musart wrote:
    > 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

    does the "status" variable print out? It would if you made it to the
    getInputStream call. What is its value? Is it a valid HTTP value? If
    not, then the server is doing it wrong and you can insist that is the
    case. Broken server is broken.
     
    Daniel Pitts, Mar 31, 2013
    #3
  4. Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    On 31/03/13 00:00, Steven Simpson wrote:
    > I would suggest calling getErrorStream() if you get an exception from
    > getInputStream(), but if the response does not appear to be valid
    > HTTP, I can't predict what getErrorStream() will actually give you.


    Of course I can; it's null if "the server sent no useful data".

    <http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getErrorStream%28%29>

    --
    ss at comp dot lancs dot ac dot uk
     
    Steven Simpson, Mar 31, 2013
    #4
  5. musart

    Guest

    Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    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
     
    , Mar 31, 2013
    #5
  6. Re: JAVA Networking: Code with getInputStream() was compatible with Java 1.6, is but anymore with 1.7

    Post the received header here.
    --
    I will not see posts from Google because I must filter them as spam
     
    Kevin McMurtrie, Mar 31, 2013
    #6
  7. musart

    telecaster

    Joined:
    Mar 31, 2013
    Messages:
    2
    Header captured with Wireshark

    HEADER CAPTURED WITH WIRESHARK (WITH BOTH JRE 1.6 & 1.7 I send and receive the same)

    SENT:
    GET /*APPLETCOMMINFO_ HTTP/1.1

    Cache-Control: no-cache

    Pragma: no-cache

    User-Agent: Java/1.6.0_43

    Host: 192.168.1.127

    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

    Connection: keep-alive



    RECEIVED:
    *APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
     
    telecaster, Mar 31, 2013
    #7
  8. musart

    Guest

    Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    HEADER CAPTURED WITH WIRESHARK (WITH BOTH JRE 1.6 & 1.7 I send and receive the same)

    SENT:
    GET /*APPLETCOMMINFO_ HTTP/1.1

    Cache-Control: no-cache

    Pragma: no-cache

    User-Agent: Java/1.6.0_43

    Host: 192.168.1.127

    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

    Connection: keep-alive



    RECEIVED:
    *APPLETCOMMINFO08404905104805405004804805104905503 2084053048049032032032032050048048050049051101000_
     
    , Mar 31, 2013
    #8
  9. musart

    Roedy Green Guest

    Re: JAVA Networking: Code with getInputStream() was compatible with Java 1.6, is but anymore with 1.7

    On Sun, 31 Mar 2013 01:37:20 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    >Status variable (int status =3D connection.getResponseCode()) is always -1 =


    -1 is what happens when you get no response whatsoever. Try cranking
    up the timeout. Retry. Check that DNS is working. Try probe using IP.
    Make sure this is not redirected to https: If so anything not kosher
    with cert or list of CAs in cacerts will derail you.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Motors make noise, and that tells you about the feelings and attitudes
    that went into it. Something was more important than sensory pleasure --
    nobody would invent a chair or dish that smelled bad or that made horrible
    noises -- why were motors invented noisy? How could they possibly be
    considered complete or successful inventions with this glaring defect?
    Unless, of course, the aggressive, hostile, assaultive sound actually served
    to express some impulse of the owner.
    ~ Philip Slater (born: 1927 age: 85)
    The Wayward Gate: Science and the Supernatural
     
    Roedy Green, Apr 1, 2013
    #9
  10. musart

    musart Guest

    Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    It worked
     
    musart, Apr 2, 2013
    #10
  11. musart

    telecaster

    Joined:
    Mar 31, 2013
    Messages:
    2
    Resolved

    Finally resolved thanks to marcos.barandun who sent me a piece of code which worked like a charm. Thanks mate!!!
     
    telecaster, Apr 2, 2013
    #11
  12. Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    On 02/04/13 22:08, musart wrote:
    > It worked


    What worked? The information you gave suggested that the server was at
    fault for not obeying HTTP, yet fixing the server was not an option for
    you. Did you drop HttpURLConnection and HTTP altogether? Did
    getErrorStream provide the response despite its promise of returning
    null if the server sent "no useful data"? Did the problem suddenly
    de-materialize?

    --
    ss at comp dot lancs dot ac dot uk
     
    Steven Simpson, Apr 3, 2013
    #12
  13. musart

    jorge Guest

    Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7

    Pat?tico tio ni puta idea de inform?tica, ded?acate a otra cosa imb?cil que bastante verguenza ajena est?s dando. Pat?tico

    Pathetic guy fucking clue about computers, another thing ded?acate moron who embarrassed enough're giving. pathetic

    Pathetic guy fucking Ahnung ?ber Computer, ded?acate andere Sache Idiot, die enough're geben verlegen. armselig
     
    jorge, Apr 5, 2013
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rollings
    Replies:
    1
    Views:
    2,284
    John C. Bollinger
    Feb 2, 2004
  2. HK
    Replies:
    3
    Views:
    4,614
    Gordon Beaton
    Mar 11, 2005
  3. Eric Lilja
    Replies:
    4
    Views:
    487
  4. ruds
    Replies:
    0
    Views:
    375
  5. pantagruel
    Replies:
    0
    Views:
    246
    pantagruel
    Feb 17, 2006
Loading...

Share This Page