سؤال

I am using apache's HttpClient (via the Fluent API). When I get the response object back, I first do:

response.returnResponse().getStatusLine().getStatusCode()

If status code is 4xx or 5xx, I throw an exception, or I return the content:

response.returnContent().asBytes();

The response here is an object of type Response. But when I run this, I am getting:

java.lang.IllegalStateException: Response content has been already consumed.

How can I get around this?

هل كانت مفيدة؟

المحلول

Both Response#returnResponse() and Response#returnContent() force the HttpResponse InputStream to be read. Since you can't read the InputStream twice, the library has put flag and a check to assert that the InputStream hasn't been consumed.

You don't get around this. What you do is get the underlying HttpResponse object and get both the status code and the body as bytes.

HttpResponse httpResponse = response.returnResponse();
httpResponse.getStatusLine().getStatusCode();
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top