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
2
  • 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? Commented May 30, 2011 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! Commented Oct 23, 2017 at 14:31

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

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
It is not the second of these. The exception is occurring within the parseHTTPHeader method.
thanks, I follow the connection.getHeaderFields()) method, it does not call parseHTTPHeader method internally
one more questions, If my server logical program need consume long times, Is it possible I get this exception.
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?

3 Comments

the addToCookieJar method is doing: cookieJar.put(new URI(providerURL), connection.getHeaderFields()); when connection.getHeaderFields() it don't read OutputStream
@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?
Thanks, I will try, but I am also confused why this error just get happened sometimes when runing some specific method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.