C#HttpWebRequest基础连接已关闭:发送时发生意外错误
我一直在谷歌上搜索并尝试所有我能找到或想到的解决方案。我尝试加载的站点正在运行TLS1.2,我尝试测试的其他几个站点也是如此,以确保它不是TLS1.2问题。其他网站加载良好C#HttpWebRequest基础连接已关闭:发送时发生意外错误,c#,httpwebrequest,C#,Httpwebrequest,我一直在谷歌上搜索并尝试所有我能找到或想到的解决方案。我尝试加载的站点正在运行TLS1.2,我尝试测试的其他几个站点也是如此,以确保它不是TLS1.2问题。其他网站加载良好 byte[] buffer = Encoding.ASCII.GetBytes( "mod=www&ssl=1&dest=account_settings.ws" + "&username=" + username.Replace(" ", "20%") + "&pas
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;
}
不管我怎么努力,我总是得到例外
基础连接已关闭:发送时发生意外错误
在我找到的更多细节下
身份验证失败,因为远程方已关闭传输流
在.Net framework的4.0版本中,仅提供以下设置:
- Ssl3:安全套接字层(SSL)3.0安全协议
- Tls:传输层安全(Tls)1.0安全协议
- Tls11:指定传输层安全(TLS)1.1安全协议
- Tls12:指定传输层安全(TLS)1.2安全协议
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
到
因此ServicePointManager将创建支持Tls12连接的流
请注意,枚举值可以用作标志,因此您可以将多个协议与逻辑OR组合在一起
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的网站(使用测试)时失败
.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;
}
}
我也有类似的问题,下面的一行帮助我解决了这个问题。感谢雷内。我只是把这一行粘贴在认证码上面,它就起作用了
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 在大多数情况下,您可能需要
Tls | Tls11 | Tls12
。通常不使用。此处定义了常量:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;