构建多部分响应

时间:2014-01-21 12:54:49

标签: c# multipart

鉴于多个HTTP请求的重新发布,我必须从中构建多部分响应。我遇到的问题是如何在最终的多部分响应中设置单个响应的响应头。对于。例如

HttpClient client = new HttpClient();
HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, "http://www.xyx/Service.svc/resource1");
HttpResponseMessage response1 = client.SendAsync(request).Result;

HttpRequestMessage request2 = new HttpRequestMessage(HttpMethod.Get, "http://www.xyx/Service.svc/resource2");
HttpResponseMessage response2 = client.SendAsync(request).Result;

MultipartContent content = new MultipartContent("mixed", "----Boundary");
content.Add(response1.Content);
content.Add(response2.Content);

我从中得到的回应就像:

------Boundary
Content-Length: 99427
Content-Type: application/json; charset=utf-8
Last-Modified: Mon, 20 Jan 2014 06:15:50 GMT

{"DebugInfo":null}
------Boundary
Content-Length: 99427
Content-Type: application/json; charset=utf-8
Last-Modified: Mon, 20 Jan 2014 06:15:50 GMT

{"DebugInfo":null}
------Boundary--

现在我想在最终响应中包含每个请求的reposne标头作为个体响应的一部分,它应该看起来像

HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 1972

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: <response-8a09ca85-8d1d-4f45-9eb0-da8e8b07ec83+1>

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/V43j6azD55CPRGb9b6uytDYl61Y"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj1/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj1/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj1",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/91POdd-sxSAkJnS8Dm7wMxBSDKk"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj2/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj2/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj2",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
ETag: "lGaP-E0memYDumK16YuUDM_6Gf0/d2Z1F1_ZVbB1dC0YKM9rX5VAgIQ"
Content-Type: application/json; charset=UTF-8
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
Content-Length: 247

{
 "kind": "storage#objectAccessControl",
 "id": "example-bucket/obj3/allUsers",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/example-bucket/o/obj3/acl/allUsers",
 "bucket": "example-bucket",
 "object": "obj3",
 "entity": "allUsers",
 "role": "READER"
}

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=--

有人知道如何在MultipartContent的{​​{1}}中添加它们吗?

1 个答案:

答案 0 :(得分:3)

知道了。您也可以向MultipartContent添加完整的回复。

MultipartContent content = new MultipartContent("mixed", "----Boundary");
content.Add(new HttpMessageContext(response1));
content.Add(new HttpMessageContext(response2));