WebClient from Asp.net gives "An existing connection was forcibly closed by the remote host" Error

I had very similar situation but different solution. On my Windows 10 dev machine+console app, the WebClient.UploadData to a https address was working just fine. But when same exact function was copied to a ASP.NET MVC app, and published to different web server (Windows 2008 R2) it was giving this exception:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send . ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

Both projects were using .NET framework 4.6.1

Solved by making the call use TLS1.2. Add this just before UploadData:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Source


It was a dns issue ... the server was resolving to the private ip console app was resolving to public