Tags

Ever tried to connect a url and failed? if there’s proxies and if you connect without considering proxies. it will fail. you cannot bypass the proxy and connect.

List<Proxy> proxyList = ProxySelector.getDefault().select(new URI("http://www.google.com")); 

This will give the list of proxies if available. [It’s possible to have more than one proxies!]

Then connect to the URL using the proxy [connect through the proxy]


URL googleUrl = new URL("http://www.google.com");

List<Proxy> proxyList = ProxySelector.getDefault().select(googleUrl.toURI());

for (Proxy proxy : proxyList) {
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
if (proxyAddress != null) {
System.out.println("Host: " + proxyAddress.getHostName() + " Port: " + proxyAddress.getPort());
URLConnection googleUrlConn = googleUrl.openConnection(proxy);
System.out.println("waiting");
System.out.println(googleUrlConn.getPermission());
googleUrlConn.connect();
Object content = googleUrlConn.getContent();
System.out.println(content);
} else {
System.out.println("No proxy available");
}

for more information check this:

http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html

Advertisements