我有一个返回 403('Forbidden')的 HttpClient 调用,但它可以通过浏览器(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
}
我努力了:
- 添加在浏览器中检测到的所有标头,而不仅仅是“用户代理”
- 将“用户代理”参数更改为“其他”
- 将 http 更改为 https
- 添加 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
这些都不能解决问题。对于其他网页,例如www.google.com,它可以正常工作。有任何想法吗?