C#HttpWebRequest底层连接已关闭:发送时发生意外错误

我一直在谷歌搜索并尝试我能找到或想到的所有解决方案。 我正在尝试加载的网站正在运行TLS1.2,我试图测试的其他几个网站也确保它不是TLS1.2问题。 其他网站装得很好。

byte[] buffer = Encoding.ASCII.GetBytes( "mod=www&ssl=1&dest=account_settings.ws" + "&username=" + username.Replace(" ", "20%") + "&password=" + password.Replace(" ", "20%")); ServicePointManager.MaxServicePointIdleTime = 1000; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create( "https://secure.runescape.com/m=weblogin/login.ws"); WebReq.Method = "POST"; WebReq.KeepAlive = false; WebReq.Referer = "https://secure.runescape.com/m=weblogin/loginform.ws" + "?mod=www&ssl=1&expired=0&dest=account_settings.ws"; WebReq.ContentType = "application/x-www-form-urlencoded"; WebReq.ContentLength = buffer.Length; Stream PostData = WebReq.GetRequestStream(); PostData.Write(buffer, 0, buffer.Length); PostData.Close(); HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream Answer = WebResp.GetResponseStream(); StreamReader _Answer = new StreamReader(Answer); reply = _Answer.ReadToEnd(); curAccount++; if (reply.Contains("Login Successful")) { eturn true; } else { eturn false; } 

无论我尝试什么,我都会得到例外

底层连接已关闭:发送时发生意外错误。

根据我发现的更多细节

身份validation失败,因为远程方已关闭传输流。

在4.Net框架的4.0版本中, ServicePointManager.SecurityProtocol仅提供了两个设置选项 :

  • Ssl3:安全套接字层(SSL)3.0安全协议。
  • Tls:传输层安全性(TLS)1.0安全协议

在框架的下一个版本中, SecurityProtocolType枚举器使用较新的Tls协议进行了扩展,因此如果您的应用程序可以使用4.5版本,您还可以使用:

  • Tls11:指定传输层安全性(TLS)1.1安全协议
  • Tls12:指定传输层安全性(TLS)1.2安全协议。

所以如果你在.Net 4.5改变你的路线

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

这样ServicePointManager将创建支持Tls12连接的流。

请注意,枚举值可以用作标志,因此您可以将多个协议与逻辑OR组合在一起

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 

注意
尽量使用尽可能低的协议数量并使用当前的安全标准保持最新。 Ssll3不再被认为是安全的,并且Tls1.0 SecurityProtocolType.Tls的使用正在下降。

我遇到了这个exception,它也与ServicePointManager.SecurityProtocol有关。

对我来说,这是因为ServicePointManager.SecurityProtocol已设置为Tls | Tls11 Tls | Tls11 (由于某些网站的应用程序访问了破坏的TLS 1.2)并且在访问仅限TLS 1.2的网站(使用SSLLabs的SSL报告进行测试)时,它失败了。

.NET 4.5及更高版本的选项是启用所有TLS版本:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 

对于.Net 4使用:

 ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; 

WebTestPlugIn的代码

 public class Protocols : WebTestPlugin { public override void PreRequest(object sender, PreRequestEventArgs e) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; } }