2

I need sent some requests to server side and get reponse, sometimes when I call specific method to run the flollowing common code, I get one error in line(addToCookieJar(connection);), any idea how this get happened?

    URL url = new URL(providerURL);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/octet-stream");

    // We understand gzip encoding
    connection.addRequestProperty("Accept-Encoding", "gzip");

    if (cookie != null && cookieHandler != null) {
        connection.setRequestProperty("Cookie", cookie);
    }

    if (cookieHandler == null) {
        addFromCookieJar(connection);
    }

    // Send the request
    ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
    oos.writeObject(remote.getName());
    oos.writeObject(m.getName()); // method name
    oos.writeObject(m.getParameterTypes()); // formal parameters
    oos.writeObject(args); // actual parameters
    oos.flush();
    oos.close();

    if (cookieHandler == null) {
        cookieJar.put(new URI(providerURL), connection.getHeaderFields());
    }

Exception:

   java.lang.reflect.UndeclaredThrowableException
            at $Proxy0.updateDocument(Unknown Source)
            at com.agst.ui.gantt.GanttPanel.doUpdateDocument(GanttPanel.java:1931)
            at com.agst.ui.gantt.GanttPanel.save(GanttPanel.java:1419)
            at com.agst.ui.gantt.GanttPanel$4.run(GanttPanel.java:1673)
            at java.lang.Thread.run(Unknown Source)

   Caused by: java.net.SocketException: Connection reset
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
            at com.agst.rmi.RemoteCallHandler.call(RemoteCallHandler.java:196)
            at com.agst.rmi.RemoteCallHandler.invoke(RemoteCallHandler.java:142)
            ... 5 more

   Caused by: java.net.SocketException: Connection reset
            at java.net.SocketInputStream.read(Unknown Source)
            at java.io.BufferedInputStream.fill(Unknown Source)
            at java.io.BufferedInputStream.read1(Unknown Source)
            at java.io.BufferedInputStream.read(Unknown Source)
            at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
            at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
            at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
            at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(Unknown Source)
            at com.agst.rmi.RemoteCallHandler.addToCookieJar(RemoteCallHandler.java:529)
            at com.agst.rmi.RemoteCallHandler.call(RemoteCallHandler.java:192)
            ... 6 more
user207421
  • 305,947
  • 44
  • 307
  • 483
Jammy
  • 31
  • 1
  • 1
  • 4
  • 1
    I have seen connection reset show up due to a bug in the F5 load balancer. If possible, try checking the connectivity as directly as possible (e.g. no Apache, no load balancer) and see if you can replicate the problem. For example, does this appear when talking to a local Tomcat? How about a local Tomcat on the next machine over? – Will Iverson May 30 '11 at 15:53
  • Can you elaborate on the F5 load balancer bug? I think I'm having the same issue with F5. Do you have any more info about this bug (F5 version, bug in some issue tracker, etc.) Thanks! – Cleankod Oct 23 '17 at 14:31

2 Answers2

2

This error indicates that the remote side has closed the connection while your side was still trying to read from it. You should check if

  • there is a problem on the server (check it's logs) or
  • you are trying to read more data than the server supplies
Waldheinz
  • 10,399
  • 3
  • 31
  • 61
  • On remote side I use protected void doPost(HttpServletRequest request, HttpServletResponse response) to handle it. And all exception are catched. I think so it should not have some problem – Jammy May 30 '11 at 14:38
  • It is not the second of these. The exception is occurring within the `parseHTTPHeader` method. – Stephen C May 30 '11 at 14:39
  • thanks, I follow the connection.getHeaderFields()) method, it does not call parseHTTPHeader method internally – Jammy May 30 '11 at 14:54
  • one more questions, If my server logical program need consume long times, Is it possible I get this exception. – Jammy May 30 '11 at 14:56
0

What does the addToCookieJar method do? Passing in the connection object might be a problem to this method since you close the OutputStream obtained from the connection. Are you by any chance trying to retrieve and operate on the output stream object in the addToCookieJar method?

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • the addToCookieJar method is doing: cookieJar.put(new URI(providerURL), connection.getHeaderFields()); when connection.getHeaderFields() it don't read OutputStream – Jammy May 30 '11 at 14:32
  • @Jammy: I still think the problem has got to do with the passed in Connection object. Can you try adding to the cookie jar *before* closing the output stream and test it out? – Sanjay T. Sharma May 30 '11 at 14:42
  • Thanks, I will try, but I am also confused why this error just get happened sometimes when runing some specific method. – Jammy May 30 '11 at 14:48