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;
budi
  • 6,351
  • 10
  • 55
  • 80
Jon
  • 1,608
  • 7
  • 25
  • 38
  • What line is throwing the exception? – BJ Myers Apr 27 '15 at 02:33
  • @Jon 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. – loneshark99 Aug 29 '16 at 18:34
  • @loneshark99 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. – Nyerguds Sep 01 '16 at 17:23
  • Ofcourse I was trying to Suggest if that is the reason that is causing it . I.e cert from server – loneshark99 Sep 01 '16 at 18:43

7 Answers7

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

Community
  • 1
  • 1
Bartho Bernsmann
  • 2,393
  • 1
  • 25
  • 34
  • 2
    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](https://tools.ietf.org/html/rfc7568) and TLS 1.0 will [likely be prohibited from June 2018](https://blog.pcisecuritystandards.org/migrating-from-ssl-and-early-tls). – Aske B. Feb 27 '18 at 08:06
  • @AskeB. I've added your comment to my answer. Thank you! – Bartho Bernsmann Mar 05 '20 at 14:25
  • 2
    If you are stucked into .net Framework 4.0 you can use `ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;` as equivalent to TLS 1.2 – Lutti Coelho Jul 31 '20 at 22:53
  • 1
    How come nobody ever says _WHERE_ this line of code is supposed to go?! :( – Scott Fraley Nov 16 '20 at 18:04
  • 2
    @ScottFraley Right before you make the actual request. – Bartho Bernsmann Nov 17 '20 at 19:45
  • 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 === – Bob Denny Jun 07 '21 at 22:47
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

Community
  • 1
  • 1
KevinVictor
  • 524
  • 1
  • 5
  • 14
  • 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. – Nyerguds Sep 01 '16 at 17:25
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.

Suamere
  • 5,691
  • 2
  • 44
  • 58
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.

Mohammad Almasi
  • 400
  • 6
  • 19
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.

clockworks
  • 3,755
  • 5
  • 37
  • 46
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

chuckc
  • 181
  • 6
-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

Dongming Yan
  • 123
  • 9