![](/img/trans.png)
[英]RestSharp.Portable inner exception “The 'Content-Length' header must be modified”
[英]RestSharp.Portable - IgnoreResponseStatusCode property does not work on UWP apps?
我目前使用RestSharp.Portable
有一段时间了,我用来设置IgnoreResponseStatusCode
中的RestClient
类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);
}
如果我没有 Internet 连接,代码会抛出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上回答了这个问题:
遗憾的是,这种行为在所有平台上都不一致,因为 HttpClient(不是 RestSharp.Portable)的所有实现都有细微的差异。 我会努力消除所有这些差异,但这可能需要一些时间。 我暂时保留这张票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.