为什么我得到java.net.SocketException:连接重置

时间:2011-05-30 14:18:16

标签: java http sockets java-ee

我需要向服务器端发送一些请求并获得响应,有时当我调用特定方法运行以下公共代码时,我得到一个错误(addToCookieJar(连接);),任何想法如何发生这种情况?< / p>

    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());
    }

例外:

   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 个答案:

答案 0 :(得分:2)

此错误表示当您的身边仍在尝试从中读取时,远程端已关闭连接。你应该检查是否

  • 服务器上有问题(检查日志)或
  • 您正在尝试阅读比服务器提供的更多数据

答案 1 :(得分:0)

addToCookieJar方法有什么作用?传递连接对象可能是此方法的问题,因为您关闭从连接获得的OutputStream。您是否有机会尝试在addToCookieJar方法中检索和操作输出流对象?