Search code examples
c#httpwebrequest

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


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.


Solution

  • In 4.0 version of the .Net framework the ServicePointManager.SecurityProtocol only offered two options to set:

    • Ssl3: Secure Socket Layer (SSL) 3.0 security protocol.
    • Tls: Transport Layer Security (TLS) 1.0 security protocol

    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:

    • Tls11: Specifies the Transport Layer Security (TLS) 1.1 security protocol
    • Tls12: Specifies the Transport Layer Security (TLS) 1.2 security protocol.

    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.