javagoogle-http-client

No content in HttpResponse even though content is sent from server


Code:

HttpContent content = createLoginContent(username, password);
GenericUrl url = new GenericUrl(serverUrl);

HttpRequest postRequest = httpRequestFactory.buildPostRequest(url, content);
postRequest.setThrowExceptionOnExecuteError(false);
HttpResponse httpResponse = postRequest.execute();

For status code OK (200), httpResponse.parseAsString() returns what I expect (body sent from server).

However, when I get a 401 status code, httpResponse.parseAsString() returns an empty string, even though server sends a 'WRONG_USERNAME_OR_PASSWORD' body.

Can I not read the body when a non-OK status code is given? How can I read the 'WRONG_USERNAME_OR_PASSWORD' message using httpResponse?

Server has been tested using RestClient-Tool to show that it does indeed send a body in its response.


Solution

  • Ok, I changed from using google-httpclient to apache-httpclient and it works much better.