This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Detecting browser proxy settings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Detecting browser proxy settings" Watch "Detecting browser proxy settings" New topic
Author

Detecting browser proxy settings

Suresh Appagundi
Ranch Hand

Joined: Sep 22, 2003
Posts: 44
Hi all,

I need to get the proxy setting details before accessing any url using URL class (java net package).

In jdk 1.5 there is a class called ProxySelector, which is supposed to do the job. But it is failing.

Below is source code iam trying

package com.httpclient;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class testURL {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedInputStream in;

HttpURLConnection conn;
BufferedReader bin;
URL u;
String fileContents="";
try {
ProxySelector ps = ProxySelector.getDefault();
URL url = new URL("http://google.com");
URLConnection connection = url.openConnection();
List proxyList = ps.select(connection.getURL().toURI());


int len = proxyList.size();
System.out.println("size : "+ len);
for (int i = 0; i < len; i++) {
Proxy p = (Proxy) proxyList.get(i);
InetSocketAddress addr = (InetSocketAddress) p.address();
if (addr == null) {
System.out.println("MANUAL");
}

else {
InetAddress ip_addr = addr.getAddress();

int tcp_port = addr.getPort();
// Use the specified IP and port number as the proxy

System.out.println("IP ADDR " + ip_addr + "tcp_port " + tcp_port);
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Please help on solving this issue. Immediate response will be of very useful.

thanks in advance.
Simon Havenith
Greenhorn

Joined: Mar 01, 2004
Posts: 4
Try adding the system property, like this

[ November 15, 2005: Message edited by: Simon Havenith ]
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
Why is this not working (JDK-1.5 option)?

How to detect Proxy Settings for Internet Connection
http://www.java-tips.org/content/view/546/2/
Simon Havenith
Greenhorn

Joined: Mar 01, 2004
Posts: 4
It works for me. What issues are you having?
 
 
subject: Detecting browser proxy settings