【发布时间】:2016-10-24 10:19:14
【问题描述】:
我们有一个 restful API(我们无法更改),如果某个部分的 Content-Type 值为 null,我们有一个特定的处理。
在 httpclient3.x 之前,我们可以执行以下操作并将内容类型设置为 null。
StringPart sp = new StringPart("name, "value")
sp.setContentType(null);
现在,我们已经转移到 http 组件(httpclient4.x jar),我意识到我们需要传递一个 ContentBody 实例(准确地说是 StringBody)。在新版本中发现了以下两种方法:
multipartEntityBuilder.addPart(FormBodyPartBuilder.create().setName(propName).setBody(new StringBody(propValue, ContentType.create("application/octet-stream", "UTF-8"))).build());
multipartEntityBuilder.addPart(propName, new StringBody(propValue, ContentType.create("application/octet-stream", "UTF-8")));
但是,我无法传递没有 Content-Type 的正文。即使我为 StringBody 使用不推荐使用的构造函数,它也会将 Content-Type 设置为纯文本。
我知道请求的任何部分都应始终具有 Content-Type,但这是我们无法升级的过时目标服务器的特殊情况。
【问题讨论】:
标签: java rest httpclient apache-httpclient-4.x apache-commons-httpclient