在.NET 3.5下建立的WinForm程序調用Core 3.0 Web API時出現「IOException: 因爲遠程方已關閉傳輸流,身份驗證失敗」錯誤html
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//IOException: 因爲遠程方已關閉傳輸流,身份驗證失敗。
錯誤詳細:ide
System.Net.WebException HResult=0x80131509 Message=基礎鏈接已經關閉: 發送時發生錯誤。 Source=<沒法計算異常源> StackTrace: <沒法計算異常堆棧跟蹤> 內部異常 1: IOException: 因爲遠程方已關閉傳輸流,身份驗證失敗。
解決辦法:spa
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();
參考:code