简体   繁体   English

如何修改HttpEntity的内容

[英]How do I modify the content of an HttpEntity

I've got an HttpResponse with an HttpEntity (all from org.apache.http ). 我有一个带有HttpEntityHttpResponse (全部来自org.apache.http )。

My HttpEntity has some application/json content that I want to modify before sending it forward. 我的HttpEntity具有一些应用程序/ json内容,我想在将其转发之前将其修改。

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. 使用toString方法用于“打印”对象。 Most of the time, you'll use the toString method for logging. 大多数时候,您将使用toString方法进行记录。 You should rather access the variables you want to read or modify via the getters. 您应该通过getter访问要读取或修改的变量。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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