This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Connection Timeout in URLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Connection Timeout in URLConnection" Watch "Connection Timeout in URLConnection" New topic
Author

Connection Timeout in URLConnection

vivek dhiman
Ranch Hand

Joined: Aug 05, 2011
Posts: 111

Hi

I have the java code which hit the web service and get the data.



But sometime this works and sometimes gives connectionTimeout exception.


Any Suggestions
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 162

Probably the fault is not the client (your code), but the server ( web service). This error means that the URL waited the specified time but didn't get any reponse so it automatically closed the connection.


Please, visit me for some cool tech post at www.ourdailycodes.com
vivek dhiman
Ranch Hand

Joined: Aug 05, 2011
Posts: 111

Thanks.

But Sometimes it connect and sometime not. Like first time if I hit its give me data when i hit second time its gives me connection timeout.Sometimes it works for 8-9 times & then connection timeout further.That is really strange for me. If this is the target server issue, then how can i debug this.

Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 162

Basically, if the problem is the server side which can't accept new connection, so if you try "telnet www.google.com 80" you will get the same problem. In other words, the server side handles to accept the connection (using a pool of threads with a listener or NIO, but the details doesn't matter) but it is too busy (all the working threads are busy to handle other connections). We can create an analogy like when you call someone phone that is already in a call. That person can be aware of the call, but he is already answering someone, so he need to finish the old call to handle the new one. If you doesn't want to wait enough (timeout parameter) you will end the call.

You can use others tools to simulate this issue creating stress tests. You can use JMeter (or SoapUI or other tool) to simulate 20 (or more) connections at the same time and see if the server side is able to answer them.
vivek dhiman
Ranch Hand

Joined: Aug 05, 2011
Posts: 111

Yes, If i am trying telnet www.google.com 80 its giving me an error Connect Failed. So what you suggest, As server from where we getting the url is of other party and we cant check on our end, we have only url.

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You seem to find it surprising that it sometimes takes more than 20 seconds to connect to that URL. Is that surprise based on actual experience with the web service? Those of us trying to answer your question don't have any experience with it, so it's quite possible that it has that sort of slow response time. In other words, don't ask us whether a response time of over 20 seconds is unexpected because we have no way of knowing.
vivek dhiman
Ranch Hand

Joined: Aug 05, 2011
Posts: 111

Ok. Thanks. Idea behind this I got
 
Consider Paul's rocket mass heater.
 
subject: Connection Timeout in URLConnection
 
Similar Threads
Parsing JSON String
Http timeout java 1.4.2
URL connection connectiontimeout and readtimeout issues
Setting timeout in URLConnection
read timeout not working after given period