File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Need help on URL connection Timeout Setings!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Need help on URL connection Timeout Setings!!" Watch "Need help on URL connection Timeout Setings!!" New topic

Need help on URL connection Timeout Setings!!

kamal shah

Joined: Feb 26, 2008
Posts: 18
I am using “” method to establish the connection. When it connects with URL and receiving no response from host, the program will hang for indefinite time.

The JDK version used is 1.4. I am seeking for the solution to define timeout at the time of connection creation. So that if scheduler will not receive any response, it will come out form the program at defined time.

Code Used for connection:

URL url = new URL(stURL);
HttpURLConnection conHttp = (HttpURLConnection)url.openConnection();

conHttp.setRequestProperty("Authorization","Basic " + encoding);

OutputStream outputStream = conHttp.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
String xmlContent = strToPost;


responseCode=conHttp.getResponseCode(); // when no response, program hangs at this statement.
if(responseCode>=203 && responseCode<=505)
//do something

InputStream inputStream = conHttp.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

String xmlResult = "";
while ((xmlResult = br.readLine()) != null)

I have tried the below solution but it didn’t work:
I have tried to set the below properties for the timeout, it is working fine with HTTP Protocol (http url’s) but doesn’t work with HTTPS(https url’s). (default: -1) (default: -1)

Please revert if you have faced the same problem or you have any idea/ solution for this issue.

Thanks n regards,

Regards,<br />Kamal<br /> <br />Life is nothing but a competition to be the criminal rather than the victim -Bertrand Russell
Dipanjan Kailthya

Joined: Jul 04, 2009
Posts: 20
You might want to consider the apache httpclient, which seems to have the timeout functionality.

There is a solution posted herefor the HTTPURLConnection problem but it looks clunky to me...

I agree. Here's the link:
subject: Need help on URL connection Timeout Setings!!
It's not a secret anymore!