Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# net如何取消/中止由GetAsync启动的web请求,传入的取消令牌只会取消任务,而不是实际的请求_C#_.net_Dotnet Httpclient - Fatal编程技术网

C# net如何取消/中止由GetAsync启动的web请求,传入的取消令牌只会取消任务,而不是实际的请求

C# net如何取消/中止由GetAsync启动的web请求,传入的取消令牌只会取消任务,而不是实际的请求,c#,.net,dotnet-httpclient,C#,.net,Dotnet Httpclient,我在.net c#中使用HttpClient的getAsync方法发出web请求,如下所示,当我使用cancellationTokenSourceForTagDetails.cancel()时,它只取消任务,不取消请求,在fiddler中,我仍然可以看到此调用发送的请求显示为挂起,只有此取消是,它不会发送第二个请求如何在.net c#中使用HttpClient类取消或中止web请求,而不仅仅是取消任务 var httpClient = new HttpClient(new HttpClient

我在.net c#中使用
HttpClient
getAsync
方法发出web请求,如下所示,当我使用
cancellationTokenSourceForTagDetails.cancel()时,它只取消任务,不取消请求,在fiddler中,我仍然可以看到此调用发送的请求显示为挂起,只有此取消是,它不会发送第二个请求如何在.net c#中使用HttpClient类取消或中止web请求,而不仅仅是取消任务

 var httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, PreAuthenticate = true });
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var result = string.Empty;
        HttpResponseMessage httpResponceMessage = null;
        ViewApplicationDiagnostics.LogTrace("GetTags Request URL is " + path);

        try
        {
            httpResponceMessage = await httpClient.GetAsync(path, cancellationTokenSourceForTagDetails.Token);
            //httpResponceMessage = await httpClient.GetAsync(path);

            if (httpResponceMessage.IsSuccessStatusCode)
            {
                result = await httpResponceMessage.Content.ReadAsStringAsync();
            }
            else
            {
                ViewApplicationDiagnostics.LogWarning("Request failed, status code is = " + httpResponceMessage.StatusCode);
                throw new Exception("Request failed with HTTP status code " + httpResponceMessage.StatusCode);
            }

        }
        catch (TaskCanceledException ex)
        {
            ViewApplicationDiagnostics.LogTrace(ex.ToString());
            throw ex;
        }

当请求被发送到服务器时-您无法再取消它。除非您可以访问服务器端的源代码,“它不会发送第二个请求”。您能详细说明一下吗?您是否希望出于某种原因发送第二个请求?是的,我使用的是基本身份验证,在基本身份验证中,它发送第一个请求,但不包含身份验证详细信息,如果失败,它将再次发送另一个包含身份验证详细信息的请求。它唯一能做的就是不发送第二个请求。。当请求被发送到服务器时-您无法再取消它。除非您可以访问服务器端的源代码,“它不会发送第二个请求”。您能详细说明一下吗?您是否希望出于某种原因发送第二个请求?是的,我使用的是基本身份验证,在基本身份验证中,它发送第一个请求,但不包含身份验证详细信息,如果失败,它将再次发送另一个包含身份验证详细信息的请求。它唯一能做的就是不发送第二个请求。。