Configure proxy and dns server for a single URLConnection

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

[2509 byte] By [buliwyfa] at [2007-11-26 22:35:00]
# 1

You should read URL class documentation:

URLConnection openConnection(Proxy proxy)

Same as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection.

Michael.Nazarov@sun.coma at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you for your answerProblem 1 (proxy) seems to be solved now, but what about the dns server. I did not find a possibility to assign different dns servers to different URLConnections
buliwyfa at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...
# 3
Why are you going to do so?
Michael.Nazarov@sun.coma at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...
# 4

Mainly because it's in the spec. I did not figure out exactly why it is necessary to use different dns servers. Let me give you a quick glimpse what the application should do:

The application checks if a URL is still available and returns an xml file with a return code (401 for example), connection time, an extract of thesource code and other information. This event repeats periodically (every 10 s, 20 min...). Therefore the customer has a good overview about the availability of his websites.

The customer himself controls the process with a xml configuration file that is read on the startup of the application. In the xml file you can find information like: connection timeout, http proxy, https proxy, used user agents, prefered request method (post or get) and regretfully even the DNS server that should be used.

Regretfully I don't have a chance to get the dns removed from the spec. The GUI which delivers the xml is already implemented and won't be changed.

buliwyfa at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...
# 5
I don't think it's possible to manage DNS using pure java. And it's bit strange requirement actually.
Michael.Nazarov@sun.coma at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...
# 6
Is it possible to tell java which DNS server should be used but as it seems like, only system wide. But as I mentioned before that's no suitable solution. Yes, I agree, the requirements are strange, but thanks anyway for your help
buliwyfa at 2007-7-10 11:43:45 > top of Java-index,Java Essentials,Java Programming...