I am using HttpClient in a xamarin forms project
The class is documented, but I can not find any documentation about which exceptions its methods might throw.
For example the GetAsync Method does not have any documentation about possible exceptions. But I assume it throws, for example when the server is unreachable.
Is there somewhere a list of exceptions this class might throw?
The basic way to handle errors in Angular is to use Angular's HttpClient service along with RxJS operators throwError and catchError. The HTTP request is made, and it returns the data with a response if anything wrong happens then it returns an error object with an error status code.
Using SendAsync, we can write the code as: static async Task SendURI(Uri u, HttpContent c) { var response = string. Empty; using (var client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage { Method = HttpMethod. Post, RequestUri = u, Content = c }; HttpResponseMessage result = await client.
As others have commented it depend on what you are calling with HttpClient. I get what you meant though and so here are some exceptions thrown with typical method calls.
SendAsync
can throw:
Task
.Source: Microsoft Docs -> HttpClient -> SendAsync
Similarly GetAsync
PostAsync
PutAsync
GetStringAsync
GetStreamAsync
etc can throw ArgumentNullException
, HttpRequestException
and as above (but not InvalidOperationException
).
Source: Microsoft Docs -> HttpClient -> GetAsync
Once you have called SendAsync
or GetAsync
etc you will have a Task<HttpResponseMessage>
. Once awaited I tend to call EnsureSuccessStatusCode()
to throw a HttpRequestException
if there is a non success HTTP status code returned. https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L161
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With