asp.net – C# HttpClient An existing connection was forcibly closed by the remote host

asp.net – C# HttpClient An existing connection was forcibly closed by the remote host

I dont see in your code sample where you are setting the value of _baseUrl, but Im assuming that is being done somewhere. Im also assuming that since this related to payments, the URL is HTTPS. If the remote host has disabled TLS 1.0 and your connection is coming in as TLS 1.0, it could cause that behavior. I know C# 4.6 has TLS 1.0/1.1/1.2 support enabled by default, but I think C# 4.6 still defaults to only SSL3/TLS 1.0 even though TLS 1.1 and 1.2 are supported. If this is the cause of the issue, you can manually add TLS 1.1 and 1.2 to the enabled values using the following code.

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

If you are using .Net 4.0 then SecurityProtocolType.Tls11 and SecurityProtocolType.Tls2 are not defined so instead you can use the hard coded value below.

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

asp.net – C# HttpClient An existing connection was forcibly closed by the remote host

It is possible to solve the issue without any changes in the code, as described in this excellent answer to a similar question:

Retarget the web project to .Net 4.6+, then update web.config as the following:

<system.web>
  <compilation targetFramework=4.6 /> 
  <httpRuntime targetFramework=4.6 /> 
</system.web>

Leave a Reply

Your email address will not be published.