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

Rob*_*Rob 28 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

根据我发现的更多细节

身份验证失败,因为远程方已关闭传输流.

ren*_*ene 89

在4.0版本的.Net框架中,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;
Run Code Online (Sandbox Code Playgroud)

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

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

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 
                                       SecurityProtocolType.Tls11 |
                                       SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

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


Sim*_*ine 16

我遇到了这个例外,它也与此有关ServicePointManager.SecurityProtocol.

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

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                     | SecurityProtocolType.Tls11
                                     | SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)


Bar*_*run 9

对于.Net 4使用:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
Run Code Online (Sandbox Code Playgroud)

  • 通常,不要使用[魔术常量](https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants). (9认同)