How to capture Httpentity on an Httpresponse more than once?

Asked

Viewed 43 times

0

In my project, I created a Helper class called Httpresponsehelper to get the body of my Httpresponse.

However, when executing the command HttpEntity httpEntity = httpResponse.getEntity() it only returns on the first call, after the second I get an error.

See my code:

public static String getResponseBody(HttpResponse httpResponse) throws IOException {
    if (httpResponse != null) {
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity);
    }
    return null;
}

Is there any way I can use EntityUtils.toString() to obtain the HttpEntity more than once?

There is a way to do what I want to do without using EntityUtils.toString(), for example using the Spring Interceptor?

If there is some way to store this value in String as if it were cached, it would be a good idea too.

  • Saving httpEntity in a backup variable would not solve your problem?

  • Erick, Thanks for the answer ! The problem is that I hope to use responseBody in several classes and not just in a specific class. Keep it in a specific variable, I’d have to keep running it through my entire code instead of just doing EntityUtils.toString(httpResponse.getEntity()) .

  • Try making this backup variable static and not the getResponseBody method();

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.