webmetrics / browsermob-proxy

NOTICE: this project has been forked and is being maintained at https://github.com/lightbody/browsermob-proxy
https://github.com/lightbody/browsermob-proxy
Apache License 2.0
234 stars 773 forks source link

Unable to read response with a HttpResponseInterceptor #15

Closed henryju closed 12 years ago

henryju commented 13 years ago

If I read the content of the response from the inside of the interceptor callback, I then receive the following error: org.openqa.selenium.WebDriverException: org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 1031; received: 0

If I only keep a reference on the HttpResponse object then try to read it after the response is completely processed, I get the following error: java.lang.RuntimeException: java.io.IOException: Attempted read from closed stream.

Maybe should you wrap the response object to allow reading the stream several times.

Thanks

benjaminhawkeslewis commented 12 years ago

You can use BufferedHttpEntity. I'm not clear on whether this might have an impact on timing information in the Har log.

proxyServer.addResponseInterceptor(
    new HttpResponseInterceptor() {

        @Override
        public void process(HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new BufferedHttpEntity(response.getEntity()));
            // do stuff with response.getEntity().getContent();
        }

    }       
);
henryju commented 12 years ago

It works fine. Many thanks.

I'm not using Har log so I don't mind the impact. ;)