Be aware about SecurityProtocolType while creating request from code

If you are using code-behind to create a HttpWebRequest like

var request = (HttpWebRequest)WebRequest.Create(Url);

and then trying to use that request to get the WebResponse like

var response = (HttpWebResponse)request.GetResponse();

This will error out as following
“HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send”

To fix that you have to setup your security protocol type.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

You can dig in here about the protocols.

Make sure you check what version of .NET framework you are using to find out different properties that is available to you.

NOTE that it affects the whole appdomain. For .NET 4.7.1, a new property is available for you where you can explicitly use it this is only available from .NET 4.7.1 and up.

Read more about it here

Hopefully, this helps.
Most of the time I write about things that did a “gotcha” to me.
My blog is just a reminder of my lessons learned.