I am currently using RestSharp.Portable
for a while now and I used to set the IgnoreResponseStatusCode
of the RestClient
class to true
in order to manage errors (especially connectivity errors) by myself.
Here a very simple example in a Windows Phone 8.1 app using the universal project and the RestSharp.Portable.HttpClient
in version 3.1.0 :
try
{
var client = new RestClient("http://www.google.fr") { IgnoreResponseStatusCode = false };
var request = new RestRequest() {Method = Method.GET };
var data = await client.Execute(request);
Debug.WriteLine(data.IsSuccess == true ? "success" : "failed");
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
If I do not have an internet connection, the code throws a System.Net.Http.HttpRequestException
exception with the message Response status code does not indicate success: 404 (Not Found).
.
And now, if I set the IgnoreResponseStatusCode
property to true
, the code does not throw any exception. This is the behavior expected, I mean the behavior I have since I use this library.
The issue appears when I am trying to execute the same code on a UWP app with the same version of the RestSharp.Portable.HttpClient
library.
It seems that the IgnoreResponseStatusCode
does not work and, if I do not have an internet connection, the code throws the following exception (no matter the value is set to true
or false
) : System.Net.Http.HttpRequestException
with the message An error occurred while sending the request.
Is there a way to do not throw an exception in this case ? Is this an issue into the RestSharp.Portable.HttpClient
library or is this a normal behavior of the UWP ?
Thx for your help.
Edit : If I use RestSharp.Portable.WebRequest
instead of RestSharp.Portable.HttpClient
it seems to work correctly
I know that my question is old and the library is not maintain anymore, but the author of the library answered the question on github directly:
The behavior is - sadly - inconsistent across all platforms, because all implementations of HttpClient (not RestSharp.Portable) have subtle differences. I'll try to iron out all those differences, but it can take some time. I'll leave this ticket open for now.