2014-01-21 43 views
2

考虑到多个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=-- 

有谁知道如何把它们在C#加入MultipartContent

回答

3

明白了。您也可以将完整的回复添加到MultipartContent

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