开发者

I want an HttpResponse object (or similar) that I can read in my own program's time without having to worry about server timeouts

开发者 https://www.devze.com 2023-01-24 07:18 出处:网络
This is the first http programming i\'ve done and I\'m self-taught so this may be a stupid question...

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 HttpResp开发者_开发技巧onse 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. BufferedHttpEntry just bufferes a proportion of the data of the stream. If you do not want the data you can use the consumeContent() 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):

public class HttpTest {
    public static void main(String... args) throws Exception {

        System.out.println(readTextPage(new URL("http://stackoverflow.com")));
    }

    private static String readTextPage(URL url) throws Exception {

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url.toURI());
        HttpResponse response = client.execute(request);


        Reader reader = null;
        try {
            reader = new InputStreamReader(response.getEntity().getContent());

            StringBuffer sb = new StringBuffer();
            {
                int read;
                char[] cbuf = new char[1024];
                while ((read = reader.read(cbuf)) != -1)
                    sb.append(cbuf, 0, read);
            }

            return sb.toString();

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消