Dear Java Programmers,
I am just getting crazy with this stuff.
I am asking for the length of a URLConnection, but sometimes the given URLOject has no information or the address doesn't exists.
URL url = new URL("http://www.alientech.to/pic/exelixis03/Auto-Exelixis_045.JPG");
URLConnection connection = url.openConnection();
//Then we ask for the length
int length = connection.getContentLength();
However, if the host is unknown the application stops in the last line without giving a time out. I have tried with a connection.setConnectTimeout(5000); but I still have the same problem.
How Can I test the validity of a URL (something like a ping) without being blocked?
If I am blocked with a URLConnection, how can interrumpt the channel ? Or even better, is it possible to close the "port" of this URLConnection ?
thanks a lot,
Marcelo
PS: I have already tried to make connection=null, but that doesn't work (the channel is still active).
Chris Smith - 30 Nov 2005 04:03 GMT
> How Can I test the validity of a URL (something like a ping) without
> being blocked?
It appears that setConnectTimeout works only for the connection to the
remote host, not for DNS lookups before the connection is made. You can
pre-attempt the DNS lookup using JNDI, and set the JNDI environment
property com.sun.jndi.dns.timeout.initial to a value in milliseconds.
Then use the normal URLConnection mechanism to set the timeout for the
communication with the remote host itself.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Marcelo - 30 Nov 2005 12:52 GMT
Hi, I have found another way to do it
HttpURLConnection.setFollowRedirects(false);
connection = (HttpURLConnection) urlObject.openConnection();
//Request Properties
connection.setRequestMethod("HEAD");
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0;H010818)" );
//Some other parameters that may help...
connection.setAllowUserInteraction( false );
connection.setDoInput( true );
connection.setDoOutput( false );
connection.setUseCaches( false );
boolean pageExists = (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
thanks for your help,
Marcelo