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;
}