URLConnection problem

M

Marcelo

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).
 
C

Chris Smith

Marcelo said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Marcelo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top