Do I need to call HttpURLConnection.disconnect after finish using it
<p>The following code basically works as expected. However, to be paranoid, I was wondering, to avoid resource leakage,</p>

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

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

<hr>

<pre><code>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)) &gt; 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);
        }
    }
}
</code></pre>
