on 06-19-2009 7:16 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
>
> 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
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
User | Count |
---|---|
80 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.