【问题标题】:How do I modify the content of an HttpEntity如何修改 HttpEntity 的内容
【发布时间】:2017-05-02 18:02:38
【问题描述】:

我有一个HttpResponse 和一个HttpEntity(都来自org.apache.http)。

我的HttpEntity 有一些应用程序/json 内容,我想在转发之前对其进行修改。

我可以将内容作为字符串读取

EntityUtils.toString(response.getEntity());

但是如何将修改后的实体内容存储回我的响应中?

【问题讨论】:

  • 看起来EntityUtils.updateEntity(...) 可能有用。

标签: java apache http httpentity


【解决方案1】:

使用EntityBuilder解决。

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

【讨论】:

    【解决方案2】:

    使用toString 方法用于“打印”一个对象。大多数时候,您将使用toString 方法进行日志记录。您应该通过 getter 访问要读取或修改的变量。例如:

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

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多