为什么我使用 RestSharp 得到 ResponseCode 0? [英] Why do I get ResponseCode 0 using RestSharp?

查看:73
本文介绍了为什么我使用 RestSharp 得到 ResponseCode 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断尝试使用各种有效的 http 代理访问此页面,但我不断收到响应代码 0

I keep trying access this page with various working http proxies and I keep getting the response code 0

var c = new RestClient("https://api.myip.com");
c.Proxy = new WebProxy(proxy.Split(":")[0], Convert.ToInt32(proxy.Split(":")[1]));
c.Timeout = 2000;
var req = new RestRequest("/", Method.GET);
var rest = c.Get(req);

我检查了代理,它们还活着并且运行良好.为什么我大部分时间都收到响应代码 0?或者有时甚至是 TemporaryRedirect 或 BadRequest,但这非常罕见.我会说 9.9/10 次我得到响应代码 0.有时它工作得很好,但非常罕见.

I've checked the proxies, they are alive and working well. Why am I getting the response code 0 for the majority of times? Or sometimes even TemporaryRedirect or BadRequest but that's very rare. I'd say 9.9/10 times I get the response code 0. And sometimes it works just fine but it's super rare.

推荐答案

您应该检查收到的响应中的更多内容.IRestResponse 实例为您提供了很多关于发生了什么的信息.RestSharp 在设计上不会抛出异常,在请求执行期间发生的任何错误都会导致响应代码 0 并且异常本身被添加到响应对象中.

You should be checking some more stuff in the response you get. The IRestResponse instance gives you quite a lot of information about what happened. RestSharp doesn't throw exceptions by design and any errors that occurred during the request execution would result in the response code 0 and the exception itself is added to the response object.

我们记录了这种行为.您可以使用许多其他属性来找出实际的响应代码和任何错误.

We have documented this behaviour. There are a bunch of other properties that you can use to find out the actual response code and any errors.

您还可以将客户端 ThrowOnAnyError 属性设置为 true 以强制 RestSharp 抛出.

You can also set the client ThrowOnAnyError property to true to force RestSharp to throw.

这篇关于为什么我使用 RestSharp 得到 ResponseCode 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆