Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp ignoring system proxy (for example Fiddler) on .NET Core

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

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

Is this a issue of RestSharp or Fiddler?

like image 492
Liu Peng Avatar asked Nov 21 '17 08:11

Liu Peng


People also ask

Is RestSharp better than HTTP client?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

What is RestSharp Netcore?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


1 Answers

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.

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

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

Update 2022-02-23: RestSharp 107 has the Proxy property moved to RestClientOptions:

var options = new RestClientOptions("http://www.google.com") {
    Proxy = new WebProxy("127.0.0.1", 8888)
};
var client = new RestClient(options);
var req = new RestRequest("/");
var resp = await client.ExecuteAsync(req);
like image 178
Alexey Zimarev Avatar answered Oct 30 '22 15:10

Alexey Zimarev