[英]Can't connect to website through C#, but with Firefox
當我嘗試連接到網站時,出現錯誤:
請求被中止:無法創建 SSL/TLS 安全通道
異常中的狀態為 SecureChannelFailure,響應為 null。
到目前為止,在 StackOverflow 上搜索答案對我沒有幫助。 我嘗試了所有解決方案:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServerCertificateValidationCallback 永遠不會被調用。
我可以毫無問題地在 Firefox 和 Edge(Chromium) 中打開網站,但 Internet Explorer 無法與其建立安全連接。 所以我假設 HttpWebRequest 使用與 Internet Explorer 相同的“機制”,但我不知道是哪個。
在https://server.cryptomix.com/secure/上,我確保我沒有提供客戶端 SSL 證書。 這意味着我不必將 ClientCertificate 添加到請求中。
這是我的整個測試代碼:
public static void Main()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(@"https://netmap.vodafone.de"));
request.Method = "GET";
request.ContentType = "text/json";
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
}
}
catch (Exception e)
{
throw;
}
}
你有什么想法,為什么我可以連接 Internet Explorer 和 HttpWebRequest,但使用 Firefox 是可能的?
運行代碼時是否遇到相同的錯誤?
編輯:我發現該網站使用的是 TLS 1.3,但沒有 SecurityProtocolType.Tls13 枚舉值。
我之前在我的一個項目中遇到過同樣的問題。 這對我有用:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
它需要System.Net
命名空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.