Jon
Jon

Reputation: 1727

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

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;

Upvotes: 17

Views: 117965

Answers (7)

Bartho Bernsmann
Bartho Bernsmann

Reputation: 2483

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

Upvotes: 44

chuckc
chuckc

Reputation: 191

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

Upvotes: 0

Mohammad Almasi
Mohammad Almasi

Reputation: 420

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.

Upvotes: 1

karfkars
karfkars

Reputation: 4063

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.

Upvotes: 0

Suamere
Suamere

Reputation: 6238

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.

Upvotes: 1

KevinVictor
KevinVictor

Reputation: 534

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

Upvotes: 11

Dongming Yan
Dongming Yan

Reputation: 117

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

Upvotes: -8

Related Questions