I have strange senario
i have HTTP response which i try to convert to string , the problem is that the first time it is working but in the second time the InputStream data is null
and i have data only in response1
InputStream is = entity.getContent();
String response1 = IOUtils.toString(is, "utf-8"); // Here every thing is fine
String respons2 = IOUtils.toString(is, "utf-8"); // Here the response2 is empty
and is (InputStream) holding no data
what is wrong here i need to be able to hold the data in the InputStream for future use in the code
Your need is not very clear however, when checking the javadoc httpEntity.getContent I see the following:
Returns a content stream of the entity. Repeatable entities are expected to create a new instance of InputStream for each invocation of this method and therefore can be consumed multiple times. Entities that are not repeatable are expected to return the same InputStream instance and therefore may not be consumed more than once.
Did you check that the httpEntity is repeatable by calling
httpEntity.isRepeatable()
if true, you could do the following:
InputStream is = entity.getContent();
String response1 = IOUtils.toString(is, "utf-8");
// retrieve a new instance of inputStream
is = entity.getContent();
String response2 = IOUtils.toString(is, "utf-8");
Last but not least (since I do not know your exact need), the previous code will helps if the entity is capable of producing its data more than once but nevertheless you should evaluate the cost between:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With