8

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?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Liu Peng
  • 1,226
  • 2
  • 11
  • 13

1 Answers1

21

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);
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • This doesn't work for me. The code runs fine, but nothing in Fiddler. Quite sure my Fiddler configuration is pretty basic. Any ideas? – Jan Aagaard May 16 '18 at 22:57
  • Not working for me either version 106.2.2, .NET Standard 2.0.1, Xamarin.Forms 3.0.0 –  May 21 '18 at 15:01
  • 1
    @Jam I tried 106.3 with explicitly specified proxy and default proxy and it works. The default proxy is only available on Windows as far as I know. – Alexey Zimarev May 31 '18 at 15:10
  • 1
    It's fine on 106.3. Thank you. –  Jun 06 '18 at 09:18
  • Up and running with proxy and credentials with dotnet core 2.2, using RestSharp 106.6.9 – Pedro Silva Mar 20 '19 at 13:35
  • Contrary to what Alexey says, I don't find that .NET Core 3.1 version of RestSharp picks up the default proxy. – NickG Jun 16 '20 at 11:48
  • 1
    ...Nothing shows up in Fiddler unless I manually configure it with the code above. .NET Core 3.1 and RestSharp 106.11.4 – NickG Jun 18 '20 at 09:30
  • ...and there is no longer client.Proxy() so it seems it's now not possible to use a proxy with Restsharp()? – NickG Feb 23 '22 at 10:37
  • Please check the migration guide. The `Proxy` property is available in the client options. – Alexey Zimarev Feb 23 '22 at 14:10