Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - HttpUrlConnection is not closing. Eventually results to SocketException

I am encountering some problems with the HttpUrlConnection in devices running Jellybean (4.1 - 4.3) wherein connections are not closed and results to a SocketException "Too many open files" after executing a number of times.

I do call HttpUrlConnection.disconnect() and am closing all the Inputstream, Outputstream, Reader and Writers in a finally block.

Going to adb shell and executing a netstat shows all the connections created by the application are left in CLOSE_WAIT state.

InputStream inputStream = httpUrlConnection.getInputStream();

// After calling inputStream.read() then the problem occurs. I think the 
// inputstream doesn't get closed even after calling close in a finally block. 
// The InputStream is a ChunkedInputStream if that helps.

I have tried other devices running on 2.3.3, 4.0.3 and 4.4 and did not encounter this issue.

Is there another way that I can manually close the connections?

like image 260
Mark Pazon Avatar asked Dec 04 '13 05:12

Mark Pazon


People also ask

How do I close HttpURLConnection?

At the most you may close the stream derived from the URLConnection. In the HTTP request-response stateless protocol, the server will automatically close the socket after sending its response, and the client will automatically close its socket after receiving the response. There is nothing more to it I think.

Do I need to disconnect HttpURLConnection?

Yes you need to close the inputstream first and close httpconnection next. As per javadoc. Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.

What is the use of HttpURLConnection in Java?

HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.

How do I set cookies in HttpURLConnection?

Setting a cookie value in a request:URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp"); URLConnection urlConn = myUrl. openConnection();


2 Answers

I finally found a workaround. It seems that Jellybean is having an issue on "Keep-Alive" connections. I just added Connection=Close to my request header and now all is working. Doing a netstat, I see that the connections are now being closed and I no longer get the SocketException due to "Too many open files".

like image 153
Mark Pazon Avatar answered Oct 16 '22 01:10

Mark Pazon


Check If you have tried all of the below... There might be something missing.. other wise it should not have any problem.

InputStream in;
HttpsURLConnection urlConnection =null;
try {
    URL url = new URL(Url);

    urlConnection = (HttpsURLConnection) url
                     .openConnection();
    //5 Second timeout
    urlConnection.setReadTimeout(5*1000);

    in = urlConnection.getInputStream();
    int responseCode = urlConnection.getResponseCode();

    if (responseCode != HttpURLConnection.HTTP_OK) {
         InputStream errInputStream = urlConnection.getErrorStream();
        //Print error message and response code..
         errInputStream.close();
    }
    in.close();
} catch (Exception e) {
    e.printStackTrace();
} finally{
    if(urlConnection != null)
        urlConnection.disconnect();
}
like image 29
Swapnil Kale Avatar answered Oct 16 '22 02:10

Swapnil Kale