Resolves the get content and status code from HttpResponse issue

Source: Internet
Author: User

When we call Apache's HTTP processing jar, we may encounter the correct state of the returned response, and then the value, which will be reported at Returncontent ().

Java.lang.IllegalStateException:Response content has been already consumedat Org.apache.http.client.fluent.Response.assertNotConsumed (response.java:56) at Org.apache.http.client.fluent.Response.handleResponse (response.java:88) at Org.apache.http.client.fluent.Response.returnContent (response.java:97)
Reason:

In the finally of Returnresponse (), the global variable consumed is set to True,returncontent () call Dispose in Handleresponse. The global variable consumed is also set to true in the final finally finally, and assertnotconsumed () is thrown out of the exception if consumed is true, so no matter which one is called first, the exception will occur.

     PublicHttpResponse Returnresponse () throws IOException {assertnotconsumed (); Try{final httpentity entity= This. response.getentity (); if(Entity! =NULL) {Final bytearrayentity bytearrayentity=Newbytearrayentity (Entityutils.tobytearray (entity)); Final ContentType ContentType=Contenttype.getordefault (entity);                Bytearrayentity.setcontenttype (Contenttype.tostring ());  This. response.setentity (bytearrayentity); }            return  This. Response; } finally {             This. consumed =true; }    }
     PublicContent returncontent ()throwsclientprotocolexception, IOException {returnHandleresponse (NewContentresponsehandler ()); }/*** Handles The response using the specified {@linkResponseHandler}*/     Public<T>T Handleresponse (FinalResponsehandler<t> handler)throwsclientprotocolexception, IOException {assertnotconsumed (); Try {            returnHandler.handleresponse ( This. Response); } finally{Dispose (); }    }Private voidDispose () {if( This. Consumed) {            return; }        Try {            Finalhttpentity entity = This. response.getentity (); FinalInputStream content =entity.getcontent (); if(Content! =NULL) {content.close (); }        } Catch(FinalException Ignore) {        } finally {             This. consumed =true; }    }
    Private void assertnotconsumed () {        if (this. Consumed) {            throw New IllegalStateException ("Response content has been already consumed");        }    }
Workaround:

In Returncontent (), the state of the response is judged first, the state value is greater than 300, the corresponding exception is thrown, and then called entityutils.tostring (Entity) value, so you can first take the HttpResponse, and then use

byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());来取Content.
        PublicContent returncontent ()throwsclientprotocolexception, IOException {returnHandleresponse (NewContentresponsehandler ()); }     Public<T>T Handleresponse (FinalResponsehandler<t> handler)throwsclientprotocolexception, IOException {assertnotconsumed (); Try {            returnHandler.handleresponse ( This. Response); } finally{Dispose (); }    }     PublicT Handleresponse (Finalhttpresponse Response)throwshttpresponseexception, IOException {FinalStatusline Statusline =Response.getstatusline (); Finalhttpentity entity =response.getentity (); if(Statusline.getstatuscode () >= 300) {Entityutils.consume (entity); Throw Newhttpresponseexception (Statusline.getstatuscode (), statusline.getreasonphrase ()); }        returnentity = =NULL?NULL: Handleentity (entity); } @Override PublicString handleentity (FinalHttpentity entity)throwsIOException {returnentityutils.tostring (entity); }

Resolves the get content and status code from HttpResponse issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.