测试代码如下
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();
}
}
}
如果不使用代理,一切正常
求科普
1
anexplore 2015-04-10 18:04:03 +08:00
|
3
zhicheng 2015-04-10 18:36:45 +08:00 via Android
一般情况是,服务器设置了 content-length 但在客户端接收到 content-length 的 body 之前服务器或代理就把连接 close 掉了。你要检查一下,服务器和代理是不是有请求大小限制或请求时间限制。
|
4
Lullaby OP 代理服务器设在日本,开了ss代理,开放了8888端口接收代理请求,现在用httpclient设置代理过去,老是报The target server failed to respond
x.x.x.x failed to respond |
6
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 |