Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

connect timed out on getResponseCode()

843790Jul 7 2009 — edited Jul 8 2009
Hi,
I have a Java application that checks if an image exists on an external server and displays a default image if I get a response code other
than 200 (Http_Ok). The parameters for the image url are from a database and they differ every time. This application is programmed to run for ever and it runs fine for 99%
of the time without any problem. 1% of the time i get the Connect timeout on HTTPURLConnection's getResponseCode() and this will cause the application to hang for ever and any further requests
are not successful until i restart the application which makes the application to run without any timeout exception even for the same URL that threw the exception previously .

URL url = "http://xyz....";
HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
respCode = httpCon.getResponseCode();

if( httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
defaultImage = image_url;
} else {

}
httpCon.disconnect();

Exception:
I/O Exceptionoccured: connect timed out
java.net.SocketTimeoutException: connect timed out
at java.lang.Throwable.<init>(Throwable.java:195)
at java.lang.Exception.<init>(Exception.java:41)
at java.io.IOException.<init>(IOException.java:40)
at java.net.SocketTimeoutException.<init>(SocketTimeoutException.java:24)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:488)
at java.net.Socket.connect(Socket.java:438)
at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:421)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:649)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:320)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:281)
at sun.net.www.http.HttpClient.New(HttpClient.java:353)
at sun.net.www.http.HttpClient.New(HttpClient.java:334)
at sun.net.www.http.HttpClient.New(HttpClient.java:329)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnecti
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:272)


Any ideas/suggestions to solve this issue ???

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 5 2009
Added on Jul 7 2009
4 comments
3,046 views