我想向"https://etebarkala.com"发送一个简单的请求。
- 尝试了从.net版本4.5到4.8,但没有成功
- 这个网站可以用浏览器很容易地打开
- 没有任何SSL验证错误或警告
结果:请求被中止:无法创建SSL/TLS安全信道。
var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://etebarkala.com"));
request.Headers.Add("Accept-Language", "en-US");
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0");
request.Headers.Add("Upgrade-Insecure-Requests", "1");
request.Headers.Add("Connection", "keep-alive");
using (HttpClientHandler handler = new HttpClientHandler())
{
handler.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;//| SslProtocols.Ssl3;//| SslProtocols.Ssl3;
handler.ServerCertificateCustomValidationCallback = (snder, cert, chain, error) => { return true; };
ServicePointManager.ServerCertificateValidationCallback = (snder, certificate, chain, errors) => { return true; };
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Ssl3 | SecurityProtocolType.SystemDefault;
using (HttpClient client = new HttpClient(handler))
{
try
{
HttpResponseMessage res = client.SendAsync(request).Result;
textBox1.Text = res.Content.ReadAsStringAsync().Result;
}
catch (Exception e)
{
string Result = e.Message;
textBox1.Text = "Error:" + Result
+ Environment.NewLine + (e.InnerException != null ? e.InnerException.Message : "")
+ Environment.NewLine + (e.InnerException != null && e.InnerException.InnerException != null ? e.InnerException.InnerException.Message : "");
}
}
}
.Result
几乎永远不是您想要做的事情;它很可能会导致您的应用程序死锁或产生其他不希望的行为。相反,您的方法应该是“async”,并且应该“await”该任务。例如:await client.SendAsync(request);
- Daniel Mann