5

I can set http proxy with this code:

public class CustomFlurlHttpClient : DefaultHttpClientFactory {
    public override HttpClient CreateClient(Url url, HttpMessageHandler m) {
        return base.CreateClient(url, CreateProxyHttpClientHandler("http://192.168.0.103:9090"));
    }

    private HttpClientHandler CreateProxyHttpClientHandler(string proxyUrl, string user = "", string passw = "") {
        NetworkCredential proxyCreds = null;
        var proxyUri = new Uri(proxyUrl);
        proxyCreds = new NetworkCredential (user, passw);
        var proxy = new WebProxy (proxyUri, false) {
            UseDefaultCredentials = false,
            Credentials = proxyCreds
        };
        var clientHandler = new HttpClientHandler {
            UseProxy = true,
            Proxy = proxy,
            PreAuthenticate = true,
            UseDefaultCredentials = false
        };
        if (user != "" && passw != "") {
            clientHandler.Credentials = new NetworkCredential (user, passw);
        }
        return clientHandler;
    }
}
class MainClass {
    public static void Main (string[] args) {
        run ();
        Console.ReadKey ();
    }

    async static void run() {
        using(FlurlClient client = new FlurlClient(c => { c.HttpClientFactory = new CustomFlurlHttpClient();})) {
            var result = await client.WithUrl("https://www.google.com").GetStringAsync();
            Console.WriteLine(result);
        };
    }
}

but not socks proxy. Any ideas how to do it? Or any other(not deprecated) rest client with async/await syntax supported?

1
  • Generally anything you can do with HttpClient, you can do with Flurl. Unfortunately I'm not finding any examples and I'm not very familiar with SOCKS personally. Maybe reframe this as an HttpClient question and someone will chime in. Apr 8, 2017 at 13:45

3 Answers 3

4

In .NET 6 you can do it easily as I answered here

But here is a quick answer:

var proxy = new WebProxy
{
    Address = new Uri("socks5://localhost:8080")
};
//proxy.Credentials = new NetworkCredential(); //Used to set Proxy logins. 
var handler = new HttpClientHandler
{
    Proxy = proxy
};
var httpClient = new HttpClient(handler);

or to configure a named HttpClient to be created using IHttpClientFactory:

Services.AddHttpClient("WithProxy")
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        var proxy = new WebProxy
        {
            Address = new Uri("socks5://localhost:8080")
        };
        return new HttpClientHandler
        {
                    Proxy = proxy
         };
    });

and when you injected IHttpClientFactory object:

httpClient = httpClientFactory.CreateClient("WithProxy");
3

Possible solution is to use Extreme.Net package, that provide socks proxy handler. For example, from code above we need to replace CreateClient method with this:

        public override HttpClient CreateClient(Url url, HttpMessageHandler m)
    {
        var socksProxy = new Socks5ProxyClient("127.0.0.1", 9150);
        var handler = new ProxyHandler(socksProxy);
        return base.CreateClient(url, handler);
    }

And it works!

3
  • Nice! That does look like it'll work, but it is possible to override the creation of just the MessageHandler and let the default factory create the HttpClient, as I'll demonstrate in a new answer. Aug 18, 2017 at 14:24
  • Extreme.Net is deprecated?
    – Toolkit
    Jan 2, 2019 at 11:52
  • according to nuget – yes
    – abritov
    Jan 3, 2019 at 12:46
2

A slightly better option is to override CreateMessageHandler instead of CreateClient in your custom factory:

public class CustomFlurlHttpClient : DefaultHttpClientFactory
{
    public override HttpMessageHandler CreateMessageHandler() {
        var socksProxy = new Socks5ProxyClient("127.0.0.1", 9150);
        return new ProxyHandler(socksProxy);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.