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.
https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netframework-4.7.2

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
https://github.com/intuit/QuickBooks-V3-DotNET-SDK/issues/54

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.

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined index: cookies in C:\hostingspaces\hoquecon\wordpress\wwwroot\wp-content\plugins\live-composer-page-builder\modules\tp-comments-form\module.php on line 1638