简体   繁体   English

如何使用java.net.URL / URLConnection与代理链

[英]How to use java.net.URL/URLConnection with a proxy-chain

I try to load websites over a chain of proxies(more then one) with java. 我尝试使用java在一系列代理(多于一个)上加载网站。 Every Request should be able to use another chain. 每个请求都应该能够使用另一个链。 Here my first quick and dirty try: 这是我第一次快速而又脏的尝试:

// 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();

As response i recived this: 作为回应我收到了这个:

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>

Lets come to the problem. 让我们来解决问题。 Now I try to load the page with URL/URLConnection to use the full handling and functinality of the java-packages. 现在我尝试使用URL / URLConnection加载页面,以使用java-packages的完整处理和功能。 Is there a way to use URL/URLConnection with a chain of proxies instead a single proxy? 有没有办法使用URL / URLConnection与代理链代替单个代理?

Thanks for the help... 谢谢您的帮助...

The HttpUrlConnection class supports proxies by using the proxy-related system settings . HttpUrlConnection类通过使用与代理相关的系统设置来支持代理。

You can, however, change the default proxy selection mechanism by calling ProxySelector.setDefault() and supplying your own ProxySelector subclass. 但是,您可以通过调用ProxySelector.setDefault()并提供自己的ProxySelector子类来更改默认代理选择机制。

By overriding the select(URL) method, you can return the desired proxy for that URL. 通过覆盖select(URL)方法,您可以返回该URL的所需代理。 I would first try to return just the 'first' proxy in the chain and see whether the HttpUrlConnection automatically makes its way through the proxy chain. 我首先尝试返回链中的“第一个”代理,看看HttpUrlConnection是否自动通过代理链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM