Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

url timeout  RSS feed

 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to socket programming. please provide some mechanism to check if response is coming before 10 seconds from url (for example www.javaranch.com) otherwise give a timeout exception.

Regards,
Neeraj.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of JDK 1.5 there URLConnection lets you set timeout parameters for connect and read. In older JDKs you're on your own.

I have one application where I just record the elapsed time. If it takes a minut, it takes a minute. Get System.currentTimeMillis() before and after to compute elapsed time. In another 1.3 application I do the connect-get-close work on another thread, join() the other thread with a timeout and throw an exception if the other thread hasn't finished reading yet.

Any of that sound useful?
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your inputs. I will try them also.

I tried with HttpClient and helped me.

Thanks,
Neeraj.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think HttpClient has a completely rewritten HttpURLConnection that adds the timeout settings. I came this close to using it instead of that extra thread trick, but my company has some odd policies about open source.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!