RestSharp忽略.NET Core上的系统代理(例如Fiddler)

时间:2017-11-21 08:27:35

标签: c# .net-core fiddler restsharp dotnet-httpclient

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

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流量,示例代码:

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的问题?

1 个答案:

答案 0 :(得分:16)

RestSharp支持系统代理,直到我们转移到.NET Standard。然后,我们在.NET Core上遇到了代理问题,然后完全删除了使用系统代理。我们有一个issue opened on Github,您可以查看那里的进展情况。

但是,显式设置代理应该适用于完整的.NET Framework,请检查this issue

该问题的代码已确认有效:

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一起测试。