简体   繁体   English

c# HttpClient 与 https 获得 400 Bad Request - 但 http 有效

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

I'm trying to get a web request working using HttpClient.我正在尝试使用 HttpClient 获取 web 请求。 It works when using http, but does not work with https.它在使用 http 时有效,但不适用于 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. 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.我测试的 URL 之一是我使用 http 和 https 设置的 URL。 This code has the same problem there as well.这段代码也有同样的问题。

Do I need to add another confuration to make HttpClient work with https?我是否需要添加另一个配置以使 HttpClient 与 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.注意:我在 Python 应用程序中使用了下面的代理,它适用于 http 和 https,所以问题不在于代理。

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谢谢你

It may have to do with the verification of ssl certificates.它可能与 ssl 证书的验证有关。 Thus, for example, you could edit the web.config file to bypass ssl errors.因此,例如,您可以编辑 web.config 文件以绕过 ssl 错误。 Like this:像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM