3

I try to load websites over a chain of proxies(more then one) with 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. Is there a way to use URL/URLConnection with a chain of proxies instead a single proxy?

Thanks for the help...

Sebastian
  • 41
  • 5
  • The `HttpUrlConnection` class supports proxies by using the [proxy-related system settings](http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies). I assume it doesn't matter whether there is just one proxy or multiple proxies, as long as you manage to configure the first one correctly. – mthmulders Apr 08 '13 at 12:15
  • Ok that should be working but I need the possibility to build a new chain for every request and many requests parallel. I fixed the question. Thanks for the response. – Sebastian Apr 08 '13 at 12:22

1 Answers1

0

The HttpUrlConnection class supports proxies by using the proxy-related system settings.

You can, however, change the default proxy selection mechanism by calling ProxySelector.setDefault() and supplying your own ProxySelector subclass.

By overriding the select(URL) method, you can return the desired proxy for that 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.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • If i read it correct, the ProxySelectorClass hold a set of proxies and return a sublist of it by executing "select(URI uri)" but how can i build and use a proxy-chain this way? – Sebastian Apr 08 '13 at 12:32
  • Setup a fix proxy-chain is'nt a solution for my problem. I did not have access to the proxy-setups. Lets say all my proxies are foreign-open-proxies. I have to build the complete proxy-chain with the request itself(or on the client-mashine). Hope i understand your advice correct. – Sebastian Apr 08 '13 at 14:27