C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send
Asked Answered
O

7

17

I'm getting this error on just one server running Windows Server 2003:

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


Here's my code... Any ideas?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:// URL HERE ");
//request.Headers.Add("Accept", "application/xml");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(xml);
request.KeepAlive = false;
request.Accept = "application/xml";
request.ContentType = "application/xml; charset='UTF-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.Timeout = 10000;
request.ServicePoint.Expect100Continue = false;
Ochs answered 27/4, 2015 at 2:28 Comment(4)
What line is throwing the exception?Judsen
@Ochs your Url is https and it looks like the Cert might not be valid. You need to accept the cert . ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; Try adding this.Tevis
@Tevis No. That is a horrible and dirty hack that should never be put in any production code. The https protocol is fully handled by HttpWebRequest anyway; there is no need to go manually messing with secure tcp connection stuff.Chiou
Ofcourse I was trying to Suggest if that is the reason that is causing it . I.e cert from serverTevis
O
-8

This problem occurs when the client computer cannot send an HTTP request. The client computer cannot send the HTTP request because the connection has been closed or is unavailable. This problem may occur when the client computer is sending lots of data. To resolve this problem, see resolutions A, D, E, F, and O.

https://support.microsoft.com/en-us/kb/915599

Oneal answered 27/4, 2015 at 2:39 Comment(6)
let us know which one of the options (A, D, E, F, O) works for youOneal
I get the error at HttpWebRequest request = (HttpWebRequest)WebRequest.Create The request is only 200 characters.Ochs
the link in the answer doesn't work anymore, is there an updated link?Zola
Microsoft being the champion of link rot since everTellurian
link is not available :(Voyageur
betaarchive.com/wiki/index.php/Microsoft_KB_Archive/915599Eolith
S
44

Setting the HttpWebRequest.KeepAlive to false didn't work for me.

Since I was accessing a HTTPS page I had to set the Service Point Security Protocol to Tls12.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Notice that there are other SecurityProtocolTypes:

SecurityProtocolType.Ssl3 
SecurityProtocolType.Tls
SecurityProtocolType.Tls11

So if the Tls12 doesn't work for you, try the three remaining options.

Also notice you can set multiple protocols. This is preferable on most cases.

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

Edit: Since this is a choice of security standards it's obviously best to go with the latest (TLS 1.2 as of writing this), and not just doing what works. In fact, SSL3 has been officially prohibited from use since 2015 and TLS 1.0 and TLS 1.1 will likely be prohibited soon as well. source: @aske-b

Slype answered 16/5, 2017 at 5:6 Comment(6)
Since this is a choice of security standards it's obviously best to go with the latest (TLS 1.2 as of writing this), and not just doing what works. In fact, SSL3 has been officially prohibited from use since 2015 and TLS 1.0 will likely be prohibited from June 2018.Suellensuelo
@AskeB. I've added your comment to my answer. Thank you!Slype
If you are stucked into .net Framework 4.0 you can use ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; as equivalent to TLS 1.2Brunhilda
How come nobody ever says WHERE this line of code is supposed to go?! :(Tributary
@ScottFraley Right before you make the actual request.Slype
Lutti Coelho THANK YOU SO MUCH!!! I am stuck on .NET 3.5 due to an audio library I cannot replace. This allows me to get RSS from sites that now require TLS12. === WONDERFUL ===Luncheon
C
11

I was getting the same error, using RestSharp with .NET 4.5. I tested the same URL with cURL and it worked fine. After a long time debugging I found that setting the SecurityProtocol fixed the issue.

See: "The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate

Curule answered 7/4, 2016 at 17:20 Comment(1)
This. It's mostly caused by websites using a higher SSL/TLS version than is enabled and/or supported by the used .Net framework.Chiou
D
1

In my case, I forgot to remove the "s" from "https" when I was swapping URLs between environments. I was hitting Localhost with https on accident. Same thing would occur if you are hitting an http site with no https certificate, or an expired certificate.

Dessertspoon answered 12/10, 2016 at 17:49 Comment(0)
I
1

This problem may occur in this case, you will want to download a link that is a filter link and you do not have permission to download that link.

Infarction answered 12/7, 2018 at 5:43 Comment(0)
D
0

I have faced with this error while I was deploying a nuget package to nexus server manually from command line with API-KEY.

I have checked the nexus server configuration and I have realized Nexus NuGet API-Key Realm is not activated. I have activated it and tried again, everythings worked fine.

enter image description here

So, You should check server side to confirm you have activated related realms.

Decorate answered 25/9, 2017 at 4:50 Comment(0)
C
0

I was getting this error trying to download an rss file with HttpWebRequest. When I tested in a browser, and checked the response codes, the url was fine. After trying everything here, it occurred to me the site might be blocking based on User Agent.

Changing the User Agent string in the request worked :

let request = WebRequest.Create(url) :?> HttpWebRequest
request.UserAgent <- @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
let response = request.GetResponse()

This User Agent String came from typing "what's my user agent" in Google Chrome

Communicate answered 28/4, 2019 at 5:8 Comment(0)
O
-8

This problem occurs when the client computer cannot send an HTTP request. The client computer cannot send the HTTP request because the connection has been closed or is unavailable. This problem may occur when the client computer is sending lots of data. To resolve this problem, see resolutions A, D, E, F, and O.

https://support.microsoft.com/en-us/kb/915599

Oneal answered 27/4, 2015 at 2:39 Comment(6)
let us know which one of the options (A, D, E, F, O) works for youOneal
I get the error at HttpWebRequest request = (HttpWebRequest)WebRequest.Create The request is only 200 characters.Ochs
the link in the answer doesn't work anymore, is there an updated link?Zola
Microsoft being the champion of link rot since everTellurian
link is not available :(Voyageur
betaarchive.com/wiki/index.php/Microsoft_KB_Archive/915599Eolith

© 2022 - 2024 — McMap. All rights reserved.