Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:34:27+00:00 2026-06-06T03:34:27+00:00

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

  • 0

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:34:29+00:00Added an answer on June 6, 2026 at 3:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the following code it basically works how I intend it to,
Is there a way to make the following code works? Basically, when I click
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
Basically it is the following code, which cannot pass compiler (g++) #include <boost/program_options.hpp> #include
I currently have the following code. Basically, just a login page and a modal
I'm struggling with the following code. Basically, I have a class Foo and nested
I write something similar to the following code a lot. It basically toggles an
I'm having an issue with rails involving javascript. Basically, I have the following code:
Following code works perfect and adds 1 and 2 values to the list, but
I have the following code. Basically I want to initialize a std::array of non-POD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.