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

问题描述

| 我需要向服务器端发送一些请求并获得响应,有时当我调用特定方法来运行下面的通用代码时,我在line(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
    

解决方法

        该错误表明远程端在尝试从连接中读取时已关闭连接。你应该检查一下 服务器上有问题(检查其日志)或 您正在尝试读取比服务器提供的更多的数据     ,        
addToCookieJar
方法有什么作用?传递连接对象可能是此方法的问题,因为您关闭了从连接获得的
OutputStream
。您是否有机会尝试在addToCookieJar方法中检索输出流对象并对其进行操作?     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...