HttpURLConnection.getResponseCode not returning
843790Nov 18 2007 — edited Nov 18 2007I have a program that every 30 seconds makes an HTTP connection to a server to deliver some data (around 3K per call). The HTTP thread runs in the region of 24-48 hours then hangs. The code where it hangs is listed below. The line where it hangs is
int code = connection.getResponseCode();
why would this call not return. Is it waiting indefinitely for a response? Other threads in the same program continue running without problem.
Thanks,
Paul
---------------------------------------------------------------------------------------------------------------------------------------------------
private static boolean trySend(CharSequence cs, String urlString)
{
boolean sent = false;
HttpURLConnection connection = null;
Writer writer = null;
try
{
URL url = new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
writer = new PrintWriter(connection.getOutputStream());
writer.append(....some text....);
writer.append(....some text....);
writer.append(....some text....);
writer.close();
int code = connection.getResponseCode();
connection.disconnect();
if (code == HttpURLConnection.HTTP_OK || code == HttpURLConnection.HTTP_CREATED)
{
sent = true;
}
else
{
getLogger().warn(String.format("HTTP response %d - %s", code, connection.getResponseMessage()));
}
}
catch (IOException e)
{
try{writer.close();}catch(Exception f){}
try{connection.disconnect();}catch(Exception f){}
getLogger().warn("Failed to send message - " + e.toString());
}
return sent;
}