23

I am writing a code that connects to websites and checks some code, like a crawler. But I need to connect trough a proxy and change the IP address (so it doesn't show the client's IP in the server logs).

How can this be done through java?

Tim
  • 239
  • 1
  • 2
  • 3

4 Answers4

49

You can use the java system properties to set up a proxy or pass it as command line options.

You can find some details and samples here.

Ex: Before opening the connection

System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");

Or you can use the default network proxies configured in the sytem

System.setProperty("java.net.useSystemProxies", "true");

Since Java 1.5 you can create a instance of proxy and pass it to the openConnection() method.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();

Or as lisak suggested, you can use some 3rd party libraries which supports your need better.

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 1
    You should also note that the proxy configuration for HTTPS uses a slightly different property eg. System.setProperty("https.proxyHost", "myProxyServer.com");. I was wondering why this wasn't working for me at first and it was because my code uses HTTPS. – talawahtech Jul 25 '14 at 05:03
  • 2
    Why is there no Proxy.Type.HTTPS (secure) constant at all? – Stefan Aug 26 '14 at 08:20
10

Or you can also use HttpClient which would suit your needs better. Check out the documentation, it's brief and very informative.

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpHost proxy = new HttpHost("someproxy", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
lisak
  • 21,611
  • 40
  • 152
  • 243
0
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();

This worked for me. I was able to use the proxy for the specific connection or transfer. Earlier we were using System.setProperty which used to set it at system level and all the requests internal/external started using the same proxy.

Also Proxy.Type.HTTP works for both http and https

0

For me it worked by adding below line before opening connection.

System.setProperty("java.net.useSystemProxies", "true");

In case you need to use a specific proxy you can even set that in system properties.

dev_2014
  • 321
  • 3
  • 6