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 828851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:47:26+00:00 2026-05-15T03:47:26+00:00

I’m new to Java development so please bear with me. Also, I hope I’m

  • 0

I’m new to Java development so please bear with me. Also, I hope I’m not the champion of tl;dr :).

I’m using HttpClient to make requests over Http (duh!) and I’d gotten it to work for a simple servlet that receives an URL as a query string parameter. I realized that my code could use some refactoring, so I decided to make my own HttpResponseHandler, to clean up the code, make it reusable and improve exception handling.

I currently have something like this:

public class HttpResponseHandler implements ResponseHandler<InputStream>{

    public InputStream handleResponse(HttpResponse response)
            throws ClientProtocolException, IOException {

        int statusCode = response.getStatusLine().getStatusCode();
        InputStream in = null;

        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpResponseException(statusCode, null);
        } else {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                in = entity.getContent();
                // This works
                // for (int i;(i = in.read()) >= 0;) System.out.print((char)i); 
            }
        }
        return in;
    }
}

And in the method where I make the actual request:

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(target);
ResponseHandler<InputStream> httpResponseHandler = new HttpResponseHandler();
try {
    InputStream in = httpclient.execute(httpget, httpResponseHandler);
    // This doesn't work
    // for (int i;(i = in.read()) >= 0;) System.out.print((char)i);     
    return in;
} catch (HttpResponseException e) {
throw new HttpResponseException(e.getStatusCode(), null);
}

The problem is that the input stream returned from the handler is closed. I don’t have any idea why, but I’ve checked it with the prints in my code (and no, I haven’t used them both at the same time :). While the first print works, the other one gives a closed stream error.

I need InputStreams, because all my other methods expect an InputStream and not a String. Also, I want to be able to retrieve images (or maybe other types of files), not just text files.

I can work around this pretty easily by giving up on the response handler (I have a working implementation that doesn’t use it), but I’m pretty curious about the following:

  1. Why does it do what it does?
  2. How do I open the stream, if something closes it?
  3. What’s the right way to do this, anyway :)?

I’ve checked the docs and I couldn’t find anything useful regarding this issue. To save you a bit of Googling, here’s the Javadoc and here’s the HttpClient tutorial (Section 1.1.8 – Response handlers).

Thanks,
Alex

  • 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-05-15T03:47:27+00:00Added an answer on May 15, 2026 at 3:47 am

    It closes the stream because ResponseHandler must handle the whole response. Even if you get an open stream, it should be at the end of stream.

    The stream is closed by BasicHttpEntity’s consumeContent() call to ensure you don’t read from the stream again.

    In your case, you don’t really need ResponseHandler.

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

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As far as I know, this is not possible in… May 15, 2026 at 2:55 pm
  • Editorial Team
    Editorial Team added an answer In your app delegate put this function (note it's not… May 15, 2026 at 2:55 pm
  • Editorial Team
    Editorial Team added an answer Simply check var myFormPanel = // get a reference to… May 15, 2026 at 2:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.