I've been Googling and trying all the solutions I could find or think of myself. The site I'm trying to load is running TLS1.2 as is a few other sites I tried to test with to make sure it wasn't a TLS1.2 issue. The other sites loaded fine.
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;
}
No matter what I try I keep getting the exception
The underlying connection was closed: An unexpected error occurred on a send.
Under more details I found
Authentication failed because the remote party has closed the transport stream.
In 4.0 version of the .Net framework the ServicePointManager.SecurityProtocol
only offered two options to set:
In the next release of the framework the SecurityProtocolType
enumerator got extended with the newer Tls protocols, so if your application can use th 4.5 version you can also use:
So if you are on .Net 4.5 change your line
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
to
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
so that the ServicePointManager will create streams that support Tls12 connections.
Do notice that the enumeration values can be used as flags so you can combine multiple protocols with a logical OR
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;
Note
Try to keep the number of protocols you support as low as possible and up-to-date with today security standards. Ssll3 is no longer deemed secure and the usage of Tls1.0 SecurityProtocolType.Tls
is in decline.
I experienced this exception, and it was also related to ServicePointManager.SecurityProtocol
.
For me, this was because ServicePointManager.SecurityProtocol
had been set to Tls | Tls11
(because of certain websites the application visits with broken TLS 1.2) and upon visiting a TLS 1.2-only website (tested with SSLLabs' SSL Report), it failed.
An option for .NET 4.5 and higher is to enable all TLS versions:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
For .Net 4 use:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
I had similar issue and the below line helped me solving the issue.Thanks to rene. I just pasted this line above the authentication code and it worked
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With