gpt4 book ai didi

c# - 构建多部分响应

转载 作者:行者123 更新时间:2023-11-30 22:09:53 24 4
gpt4 key购买 nike

考虑到多个 HTTP 请求的响应,我必须从中构建一个多部分响应。我遇到的问题是如何在最终的多部分响应中设置单个响应的响应 header 。为了。例如。

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 header 作为单个响应的一部分包括在最终响应中,它应该看起来像

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 中添加它们?

最佳答案

明白了。您还可以向 MultipartContent 添加完整的响应。

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

关于c# - 构建多部分响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com