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

How to use HttpClient.PostAsync asynchronous when the URL responds after an artificial timeout?

HttpClient.PostAsync 如何用于 post 到 URL 秒的 HTTP 请求,这些请求有人工发送回响应的时间。

请注意 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 所需的时间

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

从客户的角度来看,人为延迟与网络超时没有什么不同。所以你应该设置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;
        }
    }

However, the PostAsync times out before a response is received.

此方法超时,因为您将 HttpClient.Timeout 属性 设置为 10 秒。设置此 属性 指示客户端在指定时间后未收到响应时超时。