简体   繁体   中英

Using RestSharp with Xamarin - Execute() throws exceptions when HTTP request fails

I'm using the RestSharp 105.2.3.0 in my project which runs on Xamarin.Android 6.0.1.10.

Everything was ok, but couple of days the call to Execute() method started throwing exceptions if HTTP requests did not succeed (I guess it's related to the update of the Xamarin platform?).

For example, I receive "System.Net.WebException: The remote server returned an error: (403) Forbidden." for the 403 response status code and "System.Net.Sockets.SocketException: No route to host" if the host is unreachable.

This is weird, since on this page - https://github.com/restsharp/RestSharp/wiki/Recommended-Usage - I see:

Note that exceptions from Execute are not thrown but are available in the ErrorException property.

Also, I found a SO question which is more or less for the same issue.

The proposed solution is to set the IgnoreResponseStatusCode property:

var client = new RestClient();
client.IgnoreResponseStatusCode = true;

But I don't see this property among those available in RestClient.

How could I fix this? Of course, I can wrap the Execute call in try..catch, but I would prefer to avoid it since the Execute is not supposed to throw exceptions in the first place.

I've figured it out.

The problem was related to the fact that in the Exception settings window (Debug -> Exceptions...) in Visual Studio all checkboxes were checked in the "Break when an exception is: Thrown" column.

So the exception was caught by the RestSharp code, but VS was still breaking the execution at the Execute call as it was configured to do so.

Screenshot: http://i.stack.imgur.com/a0Fc4.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM