1

i am using getAsync method of HttpClient in .net c# to make a web request, as below, when i use cancellationTokenSourceForTagDetails.cancel() it only cancels the task but not the request, in fiddler i can still see the request sent by this call shows as pending only this cancel does is, it doesn't send second request. how to cancel or abort the web request instead of just cancelling task in .net c# using HttpClient class

 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;
        }
shyam_
  • 2,370
  • 1
  • 27
  • 50
  • 1
    When request was send to the server - you cannot to cancel it anymore. Unless you have access to the source code on the server side – Fabio Jan 23 '17 at 06:12
  • "it doesn't send second request." Can you elaborate on that? Are you expecting a second request to be sent for some reason? – Todd Menier Jan 23 '17 at 14:04
  • yes, i am using the basic authentication and in the basic authentication, it send first request without the authentication details and when that fails it again sends another request with authentication details. only this it does is that it doesnt send second rquest.. http://stackoverflow.com/questions/6338942/why-my-http-client-making-2-requests-when-i-specify-credentials – shyam_ Jan 23 '17 at 16:14

0 Answers0