c# HttpClient with https gets 400 Bad Request - but http works

Viewed 489

I'm trying to get a web request working using HttpClient. It works when using http, but does not work with https. The URL I'm testing with works in a browser with both http and https, however, when using the code below it works only with http.

One of the URLs I test with is one I setup with both http and https. This code has the same problem there as well.

Do I need to add another confuration to make HttpClient work with https?

Note: I have used the proxy below in a Python app and it works for both http and https so the problem is not the proxy.

public static async System.Threading.Tasks.Task Main(string[] args)
{
    var url = "https://www.google.com";
    using (System.Net.Http.HttpClientHandler handler = new System.Net.Http.HttpClientHandler()
    {
        Proxy = new System.Net.WebProxy(@"http://38.39.202.55:80"),
        UseProxy = true,
    })
    {
        using (System.Net.Http.HttpClient hc = new System.Net.Http.HttpClient(handler))
        {
            hc.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
            string data = "";
            using (System.Net.Http.HttpResponseMessage response = await hc.GetAsync(url))
            {
                data = await response.Content.ReadAsStringAsync();
                System.Console.WriteLine(data);
            }
            System.Console.WriteLine(data);
        }
    }
}

Thank You

1 Answers

It may have to do with the verification of ssl certificates. Thus, for example, you could edit the web.config file to bypass ssl errors. Like this:

     <system.net>
    <settings>
      <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
    </settings>
  </system.net>
Related