我已经尝试了一段时间来生成HTTP请求,就像下面报告的那样没有运气,所以我试图从StackOverFlow中获取其他人的想法。
请求应包含一个多部分正文,即HTTP Content-type标头的值为“multipart / mixed”,附件类型为octet-stream和json主体,如下所示:
·内容类型为“application / json”的JSON对象;
·Content-type为“application / octet-stream”的二进制对象。
请参阅下面我当前的代码和我在服务器端获得的输出,我还附上了我应该生成的请求。 对于附件,我尝试了FileBody和InputStreamBody,结果相同。
在服务器端,我构建了一个HttpEntity,它没有附件,只有下面报告的内容。
希望有人能够给我一个提示,因为这会变得势不可挡。
提前致谢, 埃托。
FileBody attacement = new FileBody(new File(url.getPath()));
String requestBody = "{the json string}";
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("jsonBody", requestBody,
ContentType.create("application/json"))
.addPart("att", attacement).build();
HttpPost post = new HttpPost(
"http://localhost:10191/dummy/endpoint");
post.addHeader("Content-Type", "multipart/mixed");
post.setEntity(entity);
HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse response = httpclient.execute(post);
-----------------------------------预期请求---------- -------------------------------------------------- -----
POST firmware-url HTTP/1.1
Content-Length: nnn
Content-Type: multipart/mixed;
boundary=gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU
--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU
Content-Type: application/json; charset=UTF-8
{
"firmwareUpgrade": {
"requestId": "C2F440A248C84B919124DD8CCEBF8883",
"approvedFirmwareVersionId": "ABC123123",
"deliveryPoint": [
{"commsHubId":“123346123”,"businessTargetId":"8E-62-33-A5-4F-00-44-65"},
{"commsHubId":“3454567”,"businessTargetId":"85-03-E9-E5-7F-60-72-73"}
]
}
}
--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU
Content-Type: application/octet-stream
<Binary firmware image here>
--gzC2kvz70sKw8y5teEexPrJ6RB7PKgCbvXLpU--
--------------------------发送到服务器------------------ -------------------------------------------------- ---------
--io5JwmaTY0Tww98skQZjAjJamv71dD
Content-Disposition: form-data; name="jsonBody"
Content-Type: application/json
Content-Transfer-Encoding: 8bit
{
"firmwareUpgrade": {
"requestId": "123e4567-e89b-12d3-a456-426655440000",
"approvedFirmwareVersionId": "ABC123123",
"deliveryPoint": [
{"commsHubId": 1854775807, "businessTargetId": "8E-62-33-A5-4F-00-44-65"},
{"commsHubId": 1727580722, "businessTargetId": "85-03-E9-E5-7F-60-72-73"}
]
}
}
--io5JwmaTY0Tww98skQZjAjJamv71dD
Content-Disposition: form-data; name="att"; filename="firmwareDummyImage"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
fwefwerqfndsajncvdjakvn
--io5JwmaTY0Tww98skQZjAjJamv71dD--
--------------------------发送到服务器------------------ -------------------------------------------------- ---------