This is the first http programming i’ve done and I’m self-taught so this may be a stupid question…
edit:Ultimately my question boils down to: What’s a good way to get access to all of the contents of an HttpResponse (i.e. headers + entity) but in my own sweet time? i.e. Download and then let the client process.
I’ve made a method which places an http GET request to a server. That all works fine, and I use the HttpClient.execute() function to send it off and it returns an HttpResponse object with my XML content in the entity body. My concern is that the official Apache tutorial cites the importance of ‘consuming’ the response entity content in order to free up the connection used to communicate the response. Why wouldn’t it be free? Does this mean that after execute has returned the HttpResponse but before I have done anything with it, that the client hasn’t actually received the entity body, and that only when I explicitly access the input stream that comes from it that my client actually receives the body, on some connection that has been waiting around for me to ask?
I have been using BufferedHttpEntity objects to immediately buffer the response entity to make sure that there are no timeout issues from my client not handling the response quickly enough but i’m not sure if this is necessary. When SHOULD I be using a BufferedHttpEntity?
You need to consume the whole data yourself.
BufferedHttpEntryjust bufferes a proportion of the data of the stream. If you do not want the data you can use theconsumeContent()method to ignore the rest of the input.If you do not consume the whole data the server connection will be opened in the background. The server wants to give you the data but you ignore it leaving a stream open on the server end.. Eventually the server will time out.
Maybe you do not consume all the content when getting your XML. Maybe a character is left on the server end. By calling
consumeContent()you free up this last byte.This example consumes all the content (reading the stream till it ends):