将 RestSharp 与 Xamarin 结合使用 - Execute() 在 HTTP 请求失败时抛出异常

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

我在 Xamarin.Android 6.0.1.10.

上运行的项目中使用 RestSharp 105.2.3.0

一切正常,但是几天如果 HTTP 请求不成功,对 Execute() 方法的调用开始抛出异常(我猜这与 Xamarin 平台的更新有关?)。

例如,对于 403 响应状态代码,我收到 "System.Net.WebException: The remote server returned an error: (403) Forbidden.",如果无法访问主机,则收到 "System.Net.Sockets.SocketException: No route to host"。

这很奇怪,因为在此页面上 - https://github.com/restsharp/RestSharp/wiki/Recommended-Usage - 我看到:

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

此外,我发现一个 或多或少是针对同一问题的。

建议的解决方案是设置 IgnoreResponseStatusCode 属性:

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

但是我在 RestClient 中没有看到这个 属性。

我该如何解决这个问题?当然,我可以将 Execute 调用包装在 try..catch 中,但我宁愿避免它,因为 Execute 不应该首先抛出异常。

我想通了。

问题与以下事实有关:在 Visual Studio 中的异常设置 window(调试 -> 异常...)中,所有复选框都在 "Break when an exception is: Thrown" 列中选中。

所以异常被 RestSharp 代码捕获,但 VS 仍然在 Execute 调用中中断执行,因为它被配置为这样做。

截图:http://i.stack.imgur.com/a0Fc4.png