为什么我收到 java.net.SocketException: 连接重置
我需要向服务器端发送一些请求并获得响应,有时当我调用特定方法来运行以下通用代码时,我会收到一个错误(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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
addToCookieJar
方法有什么作用?由于您关闭了从连接获取的OutputStream
,因此传入连接对象可能会成为此方法的问题。您是否有机会尝试在 addToCookieJar 方法中检索和操作输出流对象?What does the
addToCookieJar
method do? Passing in the connection object might be a problem to this method since you close theOutputStream
obtained from the connection. Are you by any chance trying to retrieve and operate on the output stream object in the addToCookieJar method?