cancel
Showing results for 
Search instead for 
Did you mean: 

Connecting to a url through proxy

Former Member
0 Kudos

Dear Experts,

In my application I have to connect to a URL using URL class, and I have to connect it using the proxy, Now, in different locations in our organization there are different proxy servers, so I want to get the proxy details from the Internet Explorer and pass it to the URL class.

Kindly suggest.

Request you to kindly provide with some code example.

Warm Regards

Upendra Agrawal

Accepted Solutions (1)

Accepted Solutions (1)

siarhei_pisarenka3
Active Contributor
0 Kudos

To inspect IE proxy settings for the target URL:

ProxySelector proxySelector = ProxySelector.getDefault();
List httpProxies = proxySelector.select(url.toURI());

To create your own proxy:

InetSocketAddress is = new InetSocketAddress(host, port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, is);

To use the proxy in HTTP connection:

URLConnection con;
if (proxy != null) {
	con = url.openConnection(proxy);
} else {
	con = url.openConnection();
}

If the proxy server requires authentication:

Authenticator.setDefault(new Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() { 
                 return new PasswordAuthentication(userName, password.toCharArray()); 
         }});

BR, Sergei

Former Member
0 Kudos

Hi Sergei,

Thanks for your reply, my proxy requires authentication, so I have to pass the username and password, as I already said, my application will run in various locations in our organisation, where there is a different proxy server, so can I get the Proxy settings such as teh host name , port , usrname & password from the users machine itself from the internet explorer.

Request you to kindly explain how it is done.

Warm regards

Upendra Agrawal

siarhei_pisarenka3
Active Contributor
0 Kudos

>

> can I get the Proxy settings such as teh host name , port , usrname & password from the users machine itself from the internet explorer.

Yep.. the method that I posted before does similar thing:

List<Proxy> proxies = proxySelector.select(url.toURI());

It'll return list of proxies configured in IE for the target URL. After you get the Proxy object you can read a proxy type and proxy address from it.

You cannot read user/password, because it's forbidden by security reasons.

How to set proxy user/password I posted already before.

BR, Sergei

Former Member
0 Kudos

Thanks again Sergei for your clarifications,

I am extremely new to this, and your post is indeed helpful, pardon me if I am asking more, but it would be very helpful to me if you provide me a working example.

Suppose I want to connect to www.google.com, and it requires proxy to connect it which is maintained in the internet explorer.

Request you to kindly help.

Warm Regards

Upendra Agrawal

Answers (0)