Hannes Fiedler wrote:
I had a strange behaviour of httpclient-3.1: When using
getResponseBodyAsStream(), my software crashed due to a IOException
caused by an attempted read-op on a closed stream, status code of the
response was okay (200). On the other hand, the exact same code worked
when i just called getResponseBodyAsString() before getting the same
again as stream (or just called getResponseBodyAsString).
Another class (written to try this https-request with httpclient) which
does essentially the same (only with hard-coded params like host,
credentials and so on, the class that actually is used is more generic)
had no problems with calling the stream-returning-method.
Can someone figure that out? I'm completely clueless ;)

That's the code of the testclass:
        try {
            HttpClient httpclient = new HttpClient();
            httpclient.getState().setCredentials(
                    new AuthScope("host",443),
                    new UsernamePasswordCredentials("login", "password"));
            PostMethod post = new PostMethod(
                    "https://url.de/path/toApp";);
             //parameters are added here via post.addParamater(key,value);
            try {
                int result = httpclient.executeMethod(post);
                System.out.println("Response status code: " + result);
                System.out.println("Response body: ");
                System.out.println(post.getResponseBodyAsString());
            } finally {
                post.releaseConnection();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

And that the one of the clas thats actually used:
        try {
            url = new URL(configuration.getURL());
            String host = url.getHost();
            int port;
            if (url.getPort() == -1) {
                port = url.getDefaultPort();
            } else {
                port = url.getPort();
            }
httpClient = new HttpClient();
            httpClient.getState().setCredentials(
                    new AuthScope(host, port),
                    new UsernamePasswordCredentials(configuration
                            .getLogin(), configuration.getPassword()));
        } catch (Exception e) {
            new ErrorMessage(e);
        }
        post = new PostMethod(url.toString());
       //adding the parameters via post.addParameter(key,value)
        int httpCode=httpClient.executeMethod(post);
        post.getResponseBodyAsString(); //without this call, the stream
is closed
        InputStream result = post.getResponseBodyAsStream();
        post.releaseConnection();

There is nothing strange about this behaviour. HttpMethod#releaseConnection makes sure the response content is fully consumed prior to releasing the connection back to the pool and closes the InputStream in order to prevent possible corruption of the underlying connection.

HttpMethod#getResponseBodyAsString method behaves differently because it creates an in-memory copy of the response body.

Consider upgrading to HttpClient 4.0.

Oleg

        return result;

Thanks in advance
hfiedler

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to