在.NET 3.5下創建的WinForm程序調用Core 3.0 Web API時出現“IOException: 由於遠程方已關閉傳輸流,身份驗證失敗”錯誤
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//IOException: 由於遠程方已關閉傳輸流,身份驗證失敗。
錯誤詳細:
System.Net.WebException HResult=0x80131509 Message=基礎連接已經關閉: 發送時發生錯誤。 Source=<無法計算異常源> StackTrace: <無法計算異常堆棧跟蹤> 內部異常 1: IOException: 由於遠程方已關閉傳輸流,身份驗證失敗。
解決辦法:
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072; //4.5版本以上可使用:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
參考:
