我一直在谷歌搜索并尝试我能找到或想到的所有解决方案。我正在尝试加载的网站正在运行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;
}
无论我尝试什么,我都会得到例外
底层连接已关闭:发送时发生意外错误。
根据我发现的更多细节
身份验证失败,因为远程方已关闭传输流。
在4.0版本的.Net框架中,ServicePointManager.SecurityProtocol
只提供two options来设置:
在框架的下一个版本中,SecurityProtocolType
枚举器扩展了更新的Tls协议,因此如果您的应用程序可以使用4.5版本,您还可以使用:
所以如果你在.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
的使用率正在下降。
我遇到了这个例外,它也与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;
}
}