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

发布于 2024-11-11 06:57:04 字数 3447 浏览 0 评论 0原文

我需要向服务器端发送一些请求并获得响应,有时当我调用特定方法来运行以下通用代码时,我会收到一个错误(addToCookieJar(connection);),知道这是怎么发生的吗?

    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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2024-11-18 06:57:04

此错误表明远程端已关闭连接,而您端仍在尝试读取连接。您应该检查

  • 服务器上是否存在问题(检查其日志)或者
  • 您正在尝试读取比服务器提供的数据更多的数据

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
厌倦 2024-11-18 06:57:04

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文