Browser HttpClient返回403(';禁止';),但通过浏览器工作(两者都使用代理服务器)

Browser HttpClient返回403(';禁止';),但通过浏览器工作(两者都使用代理服务器),browser,dotnet-httpclient,http-status-code-403,http-proxy,proxy.pac,Browser,Dotnet Httpclient,Http Status Code 403,Http Proxy,Proxy.pac,我有一个HttpClient调用,返回403(“禁止”),但它通过浏览器(Chrome,Edge)工作。这两种情况都涉及代理服务器(使用pac文件)。代码如下所示: var proxy = new WebProxy { Address = new Uri($"{proxyHost}"), BypassProxyOnLocal = false, UseDefaultCredentials = false, Credentials = new Ne

我有一个HttpClient调用,返回403(“禁止”),但它通过浏览器(Chrome,Edge)工作。这两种情况都涉及代理服务器(使用pac文件)。代码如下所示:

var proxy = new WebProxy
{
    Address = new Uri($"{proxyHost}"),
    BypassProxyOnLocal = false,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(
        userName: proxyUserName,
        password: proxyPassword)
};
var cookieContainer = new CookieContainer();
var httpClientHandler = new HttpClientHandler
{
    Proxy = proxy,
    CookieContainer = cookieContainer
};
using HttpClient client = new HttpClient(httpClientHandler);
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");
Uri uri = new Uri("http://www.etoro.com/");
HttpResponseMessage result = await client.GetAsync(uri);
StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Date: Mon, 17 May 2021 02:10:47 GMT
  Transfer-Encoding: chunked
  Connection: close
  CF-Chl-Bypass: 1
  Permissions-Policy: accelerometer=(),autoplay=(),camera=(),clipboard-read=(),clipboard-write=(),fullscreen=(),geolocation=(),gyroscope=(),hid=(),interest-cohort=(),magnetometer=(),microphone=(),payment=(),publickey-credentials-get=(),screen-wake-lock=(),serial=(),sync-xhr=(),usb=()
  Cache-Control: no-store, must-revalidate, no-cache, max-age=0, private, post-check=0, pre-check=0
  X-Frame-Options: SAMEORIGIN
  cf-request-id: 0a19b027030000736322b98000000001
  Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
  Vary: Accept-Encoding
  Strict-Transport-Security: max-age=15552000; includeSubDomains; preload
  X-Content-Type-Options: nosniff
  Server: cloudflare
  CF-RAY: 65094fb80aa77363-CPH
  Alt-Svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400
  Content-Type: text/html; charset=UTF-8
  Expires: Thu, 01 Jan 1970 00:00:01 GMT
  Content-Encoding: br
}
输出如下所示:

var proxy = new WebProxy
{
    Address = new Uri($"{proxyHost}"),
    BypassProxyOnLocal = false,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(
        userName: proxyUserName,
        password: proxyPassword)
};
var cookieContainer = new CookieContainer();
var httpClientHandler = new HttpClientHandler
{
    Proxy = proxy,
    CookieContainer = cookieContainer
};
using HttpClient client = new HttpClient(httpClientHandler);
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");
Uri uri = new Uri("http://www.etoro.com/");
HttpResponseMessage result = await client.GetAsync(uri);
StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Date: Mon, 17 May 2021 02:10:47 GMT
  Transfer-Encoding: chunked
  Connection: close
  CF-Chl-Bypass: 1
  Permissions-Policy: accelerometer=(),autoplay=(),camera=(),clipboard-read=(),clipboard-write=(),fullscreen=(),geolocation=(),gyroscope=(),hid=(),interest-cohort=(),magnetometer=(),microphone=(),payment=(),publickey-credentials-get=(),screen-wake-lock=(),serial=(),sync-xhr=(),usb=()
  Cache-Control: no-store, must-revalidate, no-cache, max-age=0, private, post-check=0, pre-check=0
  X-Frame-Options: SAMEORIGIN
  cf-request-id: 0a19b027030000736322b98000000001
  Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
  Vary: Accept-Encoding
  Strict-Transport-Security: max-age=15552000; includeSubDomains; preload
  X-Content-Type-Options: nosniff
  Server: cloudflare
  CF-RAY: 65094fb80aa77363-CPH
  Alt-Svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400
  Content-Type: text/html; charset=UTF-8
  Expires: Thu, 01 Jan 1970 00:00:01 GMT
  Content-Encoding: br
}
我试过:

  • 添加浏览器中检测到的所有标题,而不仅仅是“用户代理”
  • 将“用户代理”-参数更改为“其他”
  • 将http更改为https
  • 添加ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12
所有这些都不能解决问题。与其他网页,例如,它的作品很好。有什么想法吗