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