RestSharp.Portable - IgnoreResponseStatusCode 属性 不适用于 UWP 应用程序?

RestSharp.Portable - IgnoreResponseStatusCode property does not work on UWP apps?

我目前使用 RestSharp.Portable 有一段时间了,我曾经将 RestClient class 的 IgnoreResponseStatusCode 设置为 true 以便自己管理错误(尤其是连接错误)。

这里是 Windows Phone 8.1 应用程序中的一个非常简单的示例,该应用程序使用通用项目和版本 3.1.0 中的 RestSharp.Portable.HttpClient :

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);
}

如果我没有互联网连接,代码会抛出 System.Net.Http.HttpRequestException 异常并显示消息 Response status code does not indicate success: 404 (Not Found).

现在,如果我将 IgnoreResponseStatusCode 属性 设置为 true,代码不会抛出任何异常。这是预期的行为,我指的是自从我使用该库以来的行为。

当我尝试在具有相同版本 RestSharp.Portable.HttpClient 库的 UWP 应用程序上执行相同代码时出现此问题。

似乎 IgnoreResponseStatusCode 不起作用,如果我没有互联网连接,代码会抛出以下异常(无论值设置为 true 还是 false) : System.Net.Http.HttpRequestException 消息 An error occurred while sending the request.

有没有办法在这种情况下不抛出异常?这是 RestSharp.Portable.HttpClient 库的问题还是 UWP 的正常行为?

谢谢你的帮助。

编辑:如果我使用 RestSharp.Portable.WebRequest 而不是 RestSharp.Portable.HttpClient 它似乎工作正常

我知道我的问题很老了,图书馆也不再维护了,但是图书馆的作者直接在github上回答了这个问题:

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.