关于 http 代理请求返回 Unexpected end of file from server?

2015-04-10 16:50:10 +08:00
 Lullaby

测试代码如下
public static void proxy() throws IOException {
URL url = new URL("http://www.test.com/");
InetSocketAddress address = new InetSocketAddress("x.x.x.x", 8888);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
Authenticator.setDefault(new MyAuthenticator(new String("root".getBytes("UTF-8")), new String("password".getBytes("UTF-8"))));
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
InputStreamReader in = null;
BufferedReader reader = null;
try {
in = new InputStreamReader(connection.getInputStream());
reader = new BufferedReader(in);
StringBuffer buffer = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer);
} finally {
if (in != null) {
in.close();
}
if (reader != null) {
reader.close();
}
}
}
如果不使用代理,一切正常

求科普

29668 次点击
所在节点    Java
7 条回复
anexplore
2015-04-10 18:04:03 +08:00
Lullaby
2015-04-10 18:21:07 +08:00
@anexplore url是我改过的,换其他的也都一样;有可能是代理服务器的问题
zhicheng
2015-04-10 18:36:45 +08:00
一般情况是,服务器设置了 content-length 但在客户端接收到 content-length 的 body 之前服务器或代理就把连接 close 掉了。你要检查一下,服务器和代理是不是有请求大小限制或请求时间限制。
Lullaby
2015-04-10 19:02:22 +08:00
代理服务器设在日本,开了ss代理,开放了8888端口接收代理请求,现在用httpclient设置代理过去,老是报The target server failed to respond
x.x.x.x failed to respond
Lullaby
2015-04-10 19:03:04 +08:00
@zhicheng 不知道是不是你说的那样 回去研究下
anexplore
2015-04-10 19:19:30 +08:00
@Lullaby 这是stackoverflow上一个问题,希望可以有帮助
"Unexpected end of file" implies that the remote server accepted and closed the connection without sending a response. It's possible that the remote system is too busy to handle the request, or that there's a network bug that randomly drops connections.

With the information available it's impossible to say what's going wrong. If you have access to the servers in question you can use packet sniffing tools to find what exactly is sent and received, and look at logs to of the server process to see if there are any error messages.

http://stackoverflow.com/questions/19824339/java-simple-code-java-net-socketexception-unexpected-end-of-file-from-server
Lullaby
2015-04-11 14:57:21 +08:00
@anexplore
我在stackoverflow上看到过这个回答,他说服务器繁忙或网络错误没有正确返回response;
网上大部分人都说是response header错误;
不过我自己犯了一个错误,ss的server不是http代理服务,ss的client才是;
所以换成localhost:port应该就没问题了;
anyway, thanks so much

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/182859

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX