0

There is an old thread in stackoverflow about how make an HTTP request through a SOCKS proxy
But there is a problem in that solution.


For socks proxy in HttpWebRequest it tells use one of these two ways :

  1. Chilkat Software's Sock/SSL component
  2. Custom class for WebRequest

#1 this way is not free
#2 this way has many problems - First of all it is not complete - Second it does not support user-agent and cookies and accept headers.


For socks proxy in WebClient it tells use this class :

public class SocksWebClient : WebClient
    {
        public IProxyDetails ProxyDetails { get; set; }
        public string UserAgent { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest result = null;

            if (ProxyDetails != null)
            {
                if (ProxyDetails.ProxyType == ProxyType.Proxy)
                {
                    result = (HttpWebRequest)WebRequest.Create(address);
                    result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress);
                    if (!string.IsNullOrEmpty(UserAgent))
                        ((HttpWebRequest)result).UserAgent = UserAgent;
                }
                else if (ProxyDetails.ProxyType == ProxyType.Socks)
                {
                    result = SocksHttpWebRequest.Create(address);
                    result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress);
                    //TODO: implement user and password

                }
                else if (ProxyDetails.ProxyType == ProxyType.None)
                {
                    result = (HttpWebRequest)WebRequest.Create(address);
                    if (!string.IsNullOrEmpty(UserAgent))
                        ((HttpWebRequest)result).UserAgent = UserAgent;
                }
            }
            else
            {
                result = (HttpWebRequest)WebRequest.Create(address);
                if (!string.IsNullOrEmpty(UserAgent))
                    ((HttpWebRequest)result).UserAgent = UserAgent;
            }


            return result;
        }

    }

This class in not complete and no one mentioned what is IProxyDetails.


Finally i found this github way BetterHttpClient.
But it has error to work with socks5 proxies.
Here is the error :

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The underlying connection was closed: The connection was closed unexpectedly.


Please give me a solution for that and tell me how Make an HTTP/S request through a SOCKS4/5 proxy | [useragent - cookies - accept] Support?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Second result on Google for "HttpClient socks c#": https://github.com/MihaZupan/HttpToSocks5Proxy – ProgrammingLlama Oct 28 '21 at 01:01
  • Or look at using .NET 6, which natively supports socks proxies, instead of .NET Framework 4.8 as you currently are: https://dotnetcoretutorials.com/2021/07/11/socks-proxy-support-in-net/ – ProgrammingLlama Oct 28 '21 at 01:04
  • @Llama Thanks - I am checking - I will back for the result. – SilverLight Oct 28 '21 at 01:19
  • It worked for HTTP, not HTTPS. Here is the error for [HTTPS](https://stackoverflow.com/questions/69747877/httptosocks5proxy-the-underlying-connection-was-closed-could-not-establish-tr#69747877). – SilverLight Oct 28 '21 at 04:05

0 Answers0