获取java.net.SocketException的原因:连接重置
我需要向服务器端发送一些请求并获得响应,有时当我调用特定的方法来运行FLOWING公共代码时,我在第行中得到一个错误(addToCookieJar(connection);),知道这是如何发生的吗获取java.net.SocketException的原因:连接重置,java,http,sockets,jakarta-ee,Java,Http,Sockets,Jakarta Ee,我需要向服务器端发送一些请求并获得响应,有时当我调用特定的方法来运行FLOWING公共代码时,我在第行中得到一个错误(addToCookieJar(connection);),知道这是如何发生的吗 URL url = new URL(providerURL); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST")
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
此错误表示远程端已关闭连接,而您的端仍在尝试读取连接。你应该检查一下
- 服务器出现问题(检查日志)或
- 您试图读取的数据超过了服务器提供的数据量
OutputStream
。您是否有机会尝试在addToCookieJar方法中检索和操作输出流对象?addToCookieJar方法正在执行:cookieJar.put(新URI(providerURL),connection.getHeaderFields());当connection.getHeaderFields()时,它不读取OutputStream@Jammy:我仍然认为问题与传入的连接对象有关。在关闭输出流并测试它之前,您可以尝试添加到cookie jar吗?谢谢,我会尝试,但我也不明白为什么在运行某些特定方法时有时会发生此错误。在远程端,我使用受保护的void doPost(HttpServletRequest请求,HttpServletResponse响应)来处理它。所有例外都被捕获。我认为这不应该有什么问题,这不是第二个问题。异常发生在parseHTTPHeader
方法中。谢谢,我遵循connection.getHeaderFields()方法,它不会在内部调用parseHTTPHeader方法。如果我的服务器逻辑程序需要花费很长时间,我是否可能遇到此异常。我看到由于F5负载平衡器中的错误而出现连接重置。如果可能,尝试尽可能直接地检查连接(例如,没有Apache,没有负载平衡器),并查看是否可以复制该问题。例如,与当地的雄猫交谈时是否会出现这种情况?下一台机器上的本地Tomcat怎么样?你能详细介绍一下F5负载平衡器的错误吗?我想我对F5也有同样的问题。你有关于这个bug的更多信息吗(F5版本,一些问题跟踪程序中的bug,等等),谢谢!