c# OpenRead 请求被中止: 未能创建 SSL/TLS 安全通道。
生活随笔
收集整理的這篇文章主要介紹了
c# OpenRead 请求被中止: 未能创建 SSL/TLS 安全通道。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以前一直用的一套看指定網頁的源代碼的,今天突然報了個錯,
“System.Net.WebException”類型的未經處理的異常在 System.dll 中發生?
其他信息: 請求被中止: 未能創建 SSL/TLS 安全通道。
private string GetWebClient(string url){string strHTML = "";WebClient myWebClient = new WebClient();Stream myStream = myWebClient.OpenRead(url);StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));strHTML = sr.ReadToEnd();myStream.Close();return strHTML;}搜了下,解決方案:
先把框架提高到4.5
然后加了一行
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
private string GetWebClient(string url){string strHTML = "";WebClient myWebClient = new WebClient();System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;Stream myStream = myWebClient.OpenRead(url);StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));strHTML = sr.ReadToEnd();myStream.Close();return strHTML;}至于加的這行,文檔:https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=net-5.0
?
?
總結
以上是生活随笔為你收集整理的c# OpenRead 请求被中止: 未能创建 SSL/TLS 安全通道。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: antimalware service
- 下一篇: C# 通过pid获取hwnd / 通过p