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

C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send

我一直在谷歌搜索并尝试所有我能找到或想到的解决方案。我尝试加载的站点是 运行 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;
}

无论我尝试什么,我总是收到异常

The underlying connection was closed: An unexpected error occurred on a send.

在我找到的更多细节下

Authentication failed because the remote party has closed the transport stream.

在.Net framework 4.0版本中ServicePointManager.SecurityProtocol only offered two options设置:

  • 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 连接的流。

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

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

备注
尽量保持您支持的协议数量尽可能少,并与当今的安全标准保持同步。 Ssll3 不再被认为是安全的,Tls1.0 SecurityProtocolType.Tls 的使用正在减少。

我遇到了这个异常,也和ServicePointManager.SecurityProtocol有关。

对我来说,这是因为 ServicePointManager.SecurityProtocol 已设置为 Tls | Tls11(因为应用程序访问的某些网站使用损坏的 TLS 1.2)并且在访问仅使用 TLS 1.2 的网站时(使用SSLLabs' SSL Report), 失败了。

.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;

    }

}

我遇到了类似的问题,下面这行帮助我解决了 issue.Thanks 到 rene 的问题。我只是将这一行粘贴到验证码上方,它起作用了

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

您的项目支持.Net Framework 4.0 和.Net Framework 4.5。 如果您有升级问题

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

而不是可以使用;

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

从 IE 启用 TLs 1.2 并添加以下内容

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;