我想使用 Apache HttpClient 发送 multipart / mixed 请求,并按如下所示请求正文,但我的代码未添加 Content-Type:multipart / related; boundary = penFL6sBQHJJUN3HA4ftqC 部分加入请求正文。
--f0Ve5iPP2ySppIcDSR6Bak
**Content-Type: multipart/related;boundary=penFL6sBQHJJUN3HA4ftqC**
--penFL6sBQHJJUN3HA4ftqC
Content-Type: application/vnd.siemens.mindsphere.meta+json
{
"type": "item",
"version": "1.0",
"payload": {
"type": "standardTimeSeries",
"version": "1.0",
"details": {
"configurationId": "1551207800470"
}
}
}
--penFL6sBQHJJUN3HA4ftqC
Content-Type: application/json
[
{
"timestamp": "2019-03-01T10:00:03.780Z",
"values": [
{
"dataPointId": "1551207763818",
"value": "10",
"qualityCode": "0"
}
]
}
]
--penFL6sBQHJJUN3HA4ftqC--
--f0Ve5iPP2ySppIcDSR6Bak--
/r/n
所以,我的代码如下,
HttpEntity entity = MultipartEntityBuilder.create()
.setMimeSubtype("mixed")
.addPart(FormBodyPartBuilder.create()
.setName("configuration")
.setBody(new StringBody(part1, ContentType.create("application/vnd.siemens.mindsphere.meta+json")))
.build())
.addPart(FormBodyPartBuilder.create()
.setName("datapoints")
.setBody(new StringBody(part2, ContentType.APPLICATION_JSON))
.build())
.build();
HttpPost post = new HttpPost("https://endpointurl");
post.addHeader("Authorization","Bearer AuthKey goes here");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
但是如上所述,这并未在请求正文中设置 Content-Type:multipart / related 。 (请参阅请求主体的最上层Content-Type) 如果我这样设置,
HttpEntity entity2 = MultipartEntityBuilder.create() .setMimeSubtype("mixed") .setContentType(ContentType.create("multipart/related"))
...
它会删除请求标头中的 multipart / mixed ,并放入multipart / related。我的要求是,如上所示,要在正文中发送包含多部分/相关内容的多部分/混合请求。在Apache HttpClient中如何做?