jQuery in Action, 2nd edition*
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
Author

Need help on URL connection Timeout Setings!!

kamal shah
Greenhorn

Joined: Feb 26, 2008
Posts: 18
I am using “java.net.HttpURLConnection” 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.setDoOutput(true);
conHttp.setRequestMethod("POST");
conHttp.setRequestProperty("Content-Type","text/xml");
conHttp.setRequestProperty("Authorization","Basic " + encoding);


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

writer.println(xmlContent);
writer.flush();
outputStream.close();


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)
{
resp+=xmlResult+"\n";
}
***************************************************************************************************************





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).

1.sun.net.client.defaultConnectTimeout (default: -1)
2.sun.net.client.defaultReadTimeout (default: -1)

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

Thanks n regards,
Kamal


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

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: http://aspose.com/file-tools
 
subject: Need help on URL connection Timeout Setings!!