简体   繁体   中英

How do I modify the content of an HttpEntity

I've got an HttpResponse with an HttpEntity (all from org.apache.http ).

My HttpEntity has some application/json content that I want to modify before sending it forward.

I can read the content as a string with

EntityUtils.toString(response.getEntity());

But how do I store the modified content of my entity back into my response?

使用EntityBuilder解决。

response.setEntity(EntityBuilder.create().setText(newText).setContentType(ContentType.APPLICATION_JSON).build());

using the toString method is used to 'print' an object. Most of the time, you'll use the toString method for logging. You should rather access the variables you want to read or modify via the getters. For example:

reponse.getEntity().getContentEncoding().getName();
reponse.getEntity().getContentEncoding().getValue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM