6

I'm attempting to connect through a SOCKS5 proxy to a webservice. Currently, my app.config is as follows:

<system.net>
 <defaultProxy enabled="true"  >
  <proxy proxyaddress="http://xxx.xxx.xxx.xxx:yyyy" bypassonlocal="True" />
 </defaultProxy>
</system.net>

This works fine for http proxies, however it just doesn't connect to SOCKS5 ones. I've googled far and wide for an answer and just can't find anyone that's solved this problem, even though I've found the question a couple of times. Anyone have any idea on how I can do this?

Dan Fuller
  • 1,133
  • 11
  • 16

4 Answers4

3

In .NET 6 you can do it easily as follows:

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 when you can inject IHttpClientFactory (recommended way):

In your ConfigureServices method:

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

Then in your controller or anywhere you can inject IHttpClientFactory object, create a new httpClient (with configured proxy) instance as follows:

httpClient = httpClientFactory.CreateClient("WithProxy");

Here is a more detailed description: https://dotnetcoretutorials.com/2021/07/11/socks-proxy-support-in-net/

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Majid
  • 3,128
  • 1
  • 26
  • 31
2

Apparently Microsoft did not build SOCKS proxy support into their web classes (see http://windows-tech.info/13/f17246e7dd4a8a2b.php)

I too am in a situation where I'd like to use the built-in .NET web functionality with a SOCKS5 proxy, and have not found any satisfactory solution. I guess my next step is to obtain the source of the .NET classes either through Reflector or from the Mono codebase and refactor it to use the ProxySocket class mentioned in the above link.

This is more work than I wanted, but seems to be the only way.

Takuan Daikon
  • 41
  • 1
  • 6
  • Any luck with that? We are having the same problem (Mentalis ProxySocket works at the Socket level), and are considering the same (ugly) workaround. – vgru Sep 18 '09 at 07:04
  • I'm sorry to say that I have not found a satisfactory solution to this. Because we have postponed the requirement for the short term, I've not yet been forced to try the workaround, so I cannot say with any certainty whether it will even work. – Takuan Daikon Oct 09 '09 at 01:19
  • Any luck with that? It's been almost two years... :) – Oscar Mederos Mar 19 '11 at 07:34
1

If you are able to use HttpClient, you can use SocksSharp.

I just followed the example and it worked for me.

var settings = new ProxySettings()
{
    Host = "xxx.xxx.xxx.xxx",
    Port = yyyy
};

using (var proxyClientHandler = new ProxyClientHandler<Socks5>(settings))
{
    using (var httpClient = new HttpClient(proxyClientHandler))
    {
        var response = await httpClient.GetAsync("http://example.com/");
    }
}
mqueirozcorreia
  • 905
  • 14
  • 33
0

In theory you can inherit the webservice class and override GetWebRequest and GetWebResponse. Then you may be able to use this c# socks class http://www.codeproject.com/Articles/5954/C-class-for-connecting-via-a-SOCKS-Proxy-Server and convert the webrequest to sockets and back again.

The quickest solution I have been able to find is to set a system wide socks proxy using WideCap. This routes all traffic on the machine via the socks proxy server.

John
  • 29,788
  • 18
  • 89
  • 130