gpt4 book ai didi

c# - 当 URL 在人为超时后响应时,如何使用 HttpClient.PostAsync 异步?

转载 作者:可可西里 更新时间:2023-11-01 16:54:59 25 4
gpt4 key购买 nike

如何才能HttpClient.PostAsync用于将 HTTP 请求发布到 URL,这些 URL 有一个人工时间来发回响应。

请注意 URL 和参数 sleep=30,它用于在发送回 HTTP 响应之前人为引入 30 秒的延迟。

        Console.WriteLine("Start time : " + DateTime.Now);

for (int i = 1; i <= 10; i++)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(@"http://fake-response.appspot.com/api/?data={Hello World}&sleep=30");
//client.BaseAddress = new Uri(@"http://fake-response.appspot.com/api/?data={Hello World}&status=200");

client.Timeout = new TimeSpan(0, 0, 60);

var parameters = new Dictionary<string, string>();
parameters["ContentType"] = "text/plain;charset=UTF-8";

//Create Task to POST to the URL
client.DefaultRequestHeaders.ExpectContinue = true;
var response = await client.PostAsync(client.BaseAddress, new FormUrlEncodedContent(parameters));

Task<bool> b1 = ProcessURLAsync(response, 1, 5, 2);
}
}

Console.WriteLine("End time : " + DateTime.Now);

需要做的是,异步 HTTP Post 需要在循环中进行,不应依赖于 URL 中指定的超时时间。但是,PostAsync 在收​​到响应之前超时。

请检查在 10 个异步 POST 的循环中 POST 2 个不同的 URL 所需的时间

我检查了HttpClient.DefaultRequestHeaders.ExpectContinue ,但我认为这对这个用例没有帮助。

最佳答案

从客户端的角度来看,人为延迟与网络超时没有什么不同。所以你应该将client.Timeout设置为最大预期人工延迟+实际网络超时时间。如果您不想阻止等待响应 - 只是不要 await 从 PostAsync 返回的任务。您可以将所有此类任务存储在某个列表中,并使用 await Task.WhenAll(yourTaskList) 等待它们全部完成。或者您可以使用 ContinueWith 在给定任务完成时执行特定操作。但是,如果您根本不关心响应 - 您无论如何都必须设置足够大的超时,否则请求将过早中止。

这里有一些示例代码可以帮助您解决问题

    static async void MakeRequests()
{
var requests = new List<Task<bool>>();
for (int i = 1; i <= 10; i++)
{
// no await here, so, not await MakeRequest(i);
requests.Add(MakeRequest(i));
}
// now all 10 requests are running in parallel
try {
await Task.WhenAll(requests);
}
catch {
// no need to handle it here - we handle all errors below
}

// if we are here, all requests are either completed or failed, inspect their results
foreach (var request in requests) {
if (request.IsCanceled) {
// failed by timeout
}
else if (request.IsFaulted) {
// failed
Log(request.Exception);
}
else {
// success
bool result = request.Result;
// handle your result here if needed
}
}
}

static async Task<bool> MakeRequest(int i) {
using (var client = new HttpClient()) {
client.BaseAddress = new Uri(@"http://fake-response.appspot.com/api/?data={Hello World}&sleep=30");
//client.BaseAddress = new Uri(@"http://fake-response.appspot.com/api/?data={Hello World}&status=200");
// no timeout here, or set to max expected delay
//client.Timeout = new TimeSpan(0, 0, 60);

var parameters = new Dictionary<string, string>();
parameters["ContentType"] = "text/plain;charset=UTF-8";

//Create Task to POST to the URL
client.DefaultRequestHeaders.ExpectContinue = true;
var response = await client.PostAsync(client.BaseAddress, new FormUrlEncodedContent(parameters));

Task<bool> b1 = ProcessURLAsync(response, 1, 5, 2);
return b1;
}
}

关于c# - 当 URL 在人为超时后响应时,如何使用 HttpClient.PostAsync 异步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933376/

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