我想使用Java通过一系列代理服务器(不止一个)来加载网站。每个请求都应该能够使用另一个代理链。这是我的第一个简单粗暴的尝试:
// My proxies and there ports marked with pIP1 pPort1, pIP2 pPort2...
Socket socket = new Socket(pIP1, pPort1);
OutputStream out = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String connectOverProxy1ToProxy2 = "CONNECT " + pIP2 + ":" + pPort2 + " HTTP/1.1\n\n";
String connectHost = "GET http://stackoverflow.com/ HTTP/1.1\n\n";
out.write((connectOverProxy1ToProxy2 + connectHost).getBytes());
out.flush();
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
out.close();
in.close();
作为回答,我收到了这个:
HTTP/1.0 200 Connection established
Proxy-agent: tinyproxy/1.8.3
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: public, max-age=30
Expires: Mon, 08 Apr 2013 11:42:08 GMT
X-Frame-Options: SAMEORIGIN
Date: Mon, 08 Apr 2013 11:41:36 GMT
Last-Modified: Mon, 08 Apr 2013 11:41:08 GMT
Vary: *
Content-Length: 182825
<!DOCTYPE html>
<html>
[..]
</html>
让我们来看看这个问题。 现在我尝试使用URL/URLConnection加载页面,以便使用Java包的全部处理和功能。是否有一种方式可以使用代理链而不是单个代理来使用URL/URLConnection?
感谢您的帮助...
HttpUrlConnection
类通过使用代理相关的系统设置来支持代理。我认为,无论是一个代理还是多个代理,只要您成功配置第一个代理,就不会有问题。 - mthmulders