Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to call HttpURLConnection.disconnect after finish using it

Tags:

android

The following code basically works as expected. However, to be paranoid, I was wondering, to avoid resource leakage,

  1. Do I need to call HttpURLConnection.disconnect, after finish its usage?
  2. Do I need to call InputStream.close?
  3. Do I need to call InputStreamReader.close?
  4. Do I need to have the following 2 line of code : httpUrlConnection.setDoInput(true) and httpUrlConnection.setDoOutput(false), just after the construction of httpUrlConnection?

The reason I ask so, is most of the examples I saw do not do such cleanup. http://www.exampledepot.com/egs/java.net/post.html and http://www.vogella.com/articles/AndroidNetworking/article.html. I just want to make sure those examples are correct as well.


public static String getResponseBodyAsString(String request) {     BufferedReader bufferedReader = null;     try {         URL url = new URL(request);         HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();         InputStream inputStream = httpUrlConnection.getInputStream();         bufferedReader = new BufferedReader(new InputStreamReader(inputStream));          int charRead = 0;         char[] buffer = new char[1024];         StringBuffer stringBuffer = new StringBuffer();         while ((charRead = bufferedReader.read(buffer)) > 0) {             stringBuffer.append(buffer, 0, charRead);         }         return stringBuffer.toString();     } catch (MalformedURLException e) {         Log.e(TAG, "", e);     } catch (IOException e) {         Log.e(TAG, "", e);     } finally {         close(bufferedReader);     }     return null; }  private static void close(Reader reader) {     if (reader != null) {         try {             reader.close();         } catch (IOException exp) {             Log.e(TAG, "", exp);         }     } } 
like image 250
Cheok Yan Cheng Avatar asked Jun 15 '12 18:06

Cheok Yan Cheng


People also ask

How do I close HttpURLConnection?

Java HttpURLConnection disconnect() Method The disconnect() is the method of HttpURLConnection class. This method is used to disconnect the server. This method cannot be used to implement for reusing other HttpURLConnection request.

Can I reuse HttpURLConnection?

You don't. You close this one and create a new one.

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

Which method of HttpURLConnection class is used to retrieve the response status from server?

Get the request method. Gets the status code from an HTTP response message. Gets the HTTP response message, if any, returned along with the response code from a server. This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance.


1 Answers

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. Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection after a request may free network resources associated with this instance but has no effect on any shared persistent connection. Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.

Next two questions answer depends on purpose of your connection. Read this link for more details.

like image 109
kosa Avatar answered Sep 18 '22 13:09

kosa