gpt4 book ai didi

c# - 如何禁用 .NET Core http 客户端中的 SSL 证书检查?

转载 作者:行者123 更新时间:2023-12-05 04:39:50 25 4
gpt4 key购买 nike

我正在尝试通过代理连接到网站。这发生在 AWS Lambda 中,使用 .NET Core SDK 使用 http 客户端。这个调用看起来很像这样:

handler = new HttpClientHandler()
{
CookieContainer = cookieContainer,
Proxy = new WebProxy(
new Uri(Environment.GetEnvironmentVariable("proxyURL"))),
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
SslProtocols = SslProtocols.Tls12,
ClientCertificateOptions = ClientCertificateOption.Manual
};

using(var client = new HttpClient(handler))
{
var content = await client.GetAsync("https://my-website.com/");
}

我无法调用“https://my-website.com/”。调用超时,没有错误消息。

但是我能够使用 Golang 访问该网站,并在 AWS Lambda 中使用 resty,跳过 TLS 检查:

client := resty.New()
resp, err := client.
SetProxy(os.Getenv("proxyURL")).
SetRetryCount(3).SetTimeout(3*time.Second).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
R().Get("https://my-website.com/")

我的问题是:如何在我的 .NET Core 代码中实现我的 Golang 代码的行为?

最佳答案

TL; DR:问题不在于证书验证,而在于安全组规则。

  1. 作为Marc Gravell请指出,DangerousAcceptAnyServerCertificateValidator 已经达到了忽略证书验证问题的目的。
  2. 在同一 VPC 和子网中的 EC2 实例中部署相同的代码后,我注意到,.NET Core 代码比 Go 代码多进行一次 HTTP 调用(尽管我仍然不明白为什么)。 IP 地址不在允许的传出流量的 IP 范围内,因此阻止请求并导致 HttpClient 超时。

关于c# - 如何禁用 .NET Core http 客户端中的 SSL 证书检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70391468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com