Hi i am using HttpClient similar to this:
public static Task AsyncStringRequest(string url, string contentType)
{
try
{
var client
The exception is thrown by task.Result:
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(...);
return client.GetStringAsync(url).ContinueWith(task =>
{
try
{
return task.Result;
}
catch (AggregateException ex)
{
throw ex;
}
catch (WebException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
});
Better: check if the task faulted before accessing task.Result:
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(...);
return client.GetStringAsync(url).ContinueWith(task =>
{
if (task.IsFaulted)
{
var ex = task.Exception;
}
else if (task.IsCancelled)
{
}
else
{
return task.Result;
}
});
If you're not actually doing something in the ContinueWith, you can simply omit it:
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(...);
return client.GetStringAsync(url);