Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Configure proxy and dns server for a single URLConnection

807606Mar 22 2007 — edited Mar 22 2007
Hello!

I am relatively new to network programming in Java so I was looking for a solution to set a DNS server and a PROXY server for one SINGLE UrlConnection. The settings should only be used in one thread, while another thread maybe has different settings.

The most suggestions I found on the net looked like follows:

Proxy:
System.setProperty("proxyPort","8080");
System.setProperty("proxyHost","proxy"
);

DNS Server
sun.net.spi.nameservice.provider.<n>=<default|dns,sun|...>
sun.net.spi.nameservice.nameservers=<server1_ipaddr,server2_ipaddr ...>
sun.net.spi.nameservice.domain=<domainname>

The problem is that both solutions are not thread safe and the settings are valid system wide (as far as I am understand). In my application a lot of different workerThreads open a connection to a remote server via URLConnection , for example:
URL url = new URL("http://www.sun.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
In the worst case 200 different workerThreads open a connection nearly at the same time. If the worst case happens one workerThread changes the DNS and PROXY settings and in the next step another thread which already has set dns and proxy tries to connect to a specified server with wrong dns and proxy settings.

One solution would be to synchronize the whole method that configures the connection and tries to connect to a remote server but that would be a very bad solution for me, because all other threads have to wait until the last thread has finished.

My question: Is there a possibility to configure a different DNS server and a different PROXY server for each URLConnection, for example something like:
URL url = new URL("http://www.sun.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setProxyHost("proxy");
con.setProxyPort("3128");
con.setProxyUsername("user");
con.setProxyPassword("pass");
con.setDNS("85.27.63.2");
Thanks in advance for your answers.

Kind regards,

Buliwyf
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2007
Added on Mar 22 2007
6 comments
1,014 views