Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

URLConnection problem

Thread view: 
Marcelo - 30 Nov 2005 01:49 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.