0

I am using a translator because I am Korean. I would appreciate your understanding even if I'm not good at it.

I would like to send a post request from c# to https web to http proxy using http proxy.

            HttpClient httpclient = null;
            proxy = "http://" + proxy;
            HttpClientHandler handler = new HttpClientHandler()
            {

                Proxy = new WebProxy(proxy),
                UseProxy = true,
            };

            httpclient = new HttpClient(handler);

this is my c# code

But I can't connect to the https web.

Is there a way to use the http client module in c# to produce the same effect as the following python code?

        proxy={ 'https' : 'http://' + input_proxy }
        resp=self.desktop.post(self.url,self.payload,proxies=proxy)
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
신유연
  • 1
  • 2
  • I recommend you to visit this link: https://stackoverflow.com/questions/29856543/httpclient-and-using-proxy-constantly-getting-407 – Hesam Akbari Mar 10 '21 at 06:25

2 Answers2

0

I recommend you to visit this link: HttpClient and using a proxy

First, create a proxy object

var proxy = new WebProxy
{
    Address = new Uri($"http://{proxyHost}:{proxyPort}"),
    BypassProxyOnLocal = false,
    UseDefaultCredentials = false,

    // *** These creds are given to the proxy server, not the web server ***
    Credentials = new NetworkCredential(
        userName: proxyUserName,
        password: proxyPassword)
};

Now create a client handler that uses that proxy

var httpClientHandler = new HttpClientHandler
{
    Proxy = proxy,
};

Omit this part if you don't need to authenticate with the webserver:

if (needServerAuthentication)
{
    httpClientHandler.PreAuthenticate = true;
    httpClientHandler.UseDefaultCredentials = false;

    // *** These creds are given to the web server, not the proxy server ***
    httpClientHandler.Credentials = new NetworkCredential(
        userName: serverUserName,
        password: serverPassword);
}

Finally, create the HTTP client object

var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
Hesam Akbari
  • 1,071
  • 1
  • 5
  • 14
  • Can access https? – 신유연 Mar 10 '21 at 07:25
  • Yes, but i think If the server only supports higher TLS version like TLS 1.2 only, it will still fail unless your client PC is configured to use higher TLS version by default. To overcome this problem add this code before setting address: `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;` `Address = new Uri($"https://{proxyHost}:{proxyPort}"), ...` – Hesam Akbari Mar 10 '21 at 14:10
-1

You can also use famous c# library for RESTAPIs communcation RESTSharp if you want to proxy use

    var client = new RestClient("http://example.com")
    client.Proxy = new WebProxy("http://proxy.example.com")
M.A.K
  • 1
  • 3