hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hannes Fiedler <mithrandir...@web.de>
Subject strange behaviour of getResponseBodyAsStream()
Date Fri, 28 Aug 2009 19:56:36 GMT
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();
        return result;

Thanks in advance
hfiedler

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message