RestSharp 忽略 .NET Core 上的系统代理(例如 Fiddler) [英] RestSharp ignoring system proxy (for example Fiddler) on .NET Core

查看:64
本文介绍了RestSharp 忽略 .NET Core 上的系统代理(例如 Fiddler)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Fiddler 检查 http 流量,但没有捕获任何 http 流量,我的测试代码:

I want check the http traffic with Fiddler, but no any http traffic captured, my testing codes:

private static void ByRestSharp()
{
    var restClient = new RestClient("https://jsonplaceholder.typicode.com");
    var request = new RestRequest("posts", Method.GET);
    var response = restClient.Get<List<Post>>(request);
    Console.WriteLine("{0} posts return by RestSharp.", response.Data.Count);
}

但是我改用HttpClient后,Fiddler就可以抓到http流量了,示例代码:

But after I changed to use HttpClient, Fiddler can capture the http traffic, sample codes:

private static void ByHttpClient()
{
    var httpClient = new HttpClient();
    using (var req = new HttpRequestMessage(HttpMethod.Get, "https://jsonplaceholder.typicode.com/posts"))
    using (var resp = httpClient.SendAsync(req).Result)
    {
        var json = resp.Content.ReadAsStringAsync().Result;
        var users = SimpleJson.SimpleJson.DeserializeObject<List<Post>>(json);
        Console.WriteLine("{0} posts return by HttpClient.", users.Count);
    }
}

这是 RestSharp 还是 Fiddler 的问题?

Is this a issue of RestSharp or Fiddler?

推荐答案

RestSharp 支持系统代理,直到我们迁移到 .NET Standard.然后,我们在 .NET Core 上遇到了代理问题,然后完全删除了系统代理的使用.我们有一个问题在 Github 上打开,您可以在那里查看进度.

RestSharp supported system proxy until we moved to .NET Standard. Then, we got issues with proxy on .NET Core and then using the system proxy was removed entirely. We have an issue opened on Github and you can check the progress there.

但是,显式设置代理应该适用于完整的 .NET Framework,请检查这个问题.

However, explicitly setting the proxy should work for full .NET Framework, check this issue.

问题中的代码,已确认有效:

Code from the issue, which is confirmed to be working:

var client = new RestClient("http://www.google.com");
client.Proxy = new WebProxy("127.0.0.1", 8888);
var req = new RestRequest("/", Method.GET);
var resp = client.Execute(req);

更新 2018-05-31:RestSharp 106.3 也在 .NET Core 上自动使用默认代理.使用 Fiddler 测试.

Update 2018-05-31: RestSharp 106.3 is using the default proxy on .NET Core also, automatically. Tested with Fiddler.

这篇关于RestSharp 忽略 .NET Core 上的系统代理(例如 Fiddler)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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