Labels

slider

Recent

Navigation

The underlying connection was closed: An unexpected error occurred on a send of web request

The underlying connection was closed: An unexpected error occurred on a send of web request1

Introduction

Usually, you could find two resolutions for the above-said error regarding ‘The underlying connection was closed: An unexpected error occurred on a send of web request’. Recently, we discussed a few articles on connection timeout in IIS, could not open a connection in SQL Server, The connection has timed out.

The underlying connection was closed: An unexpected error occurred on a send of web request

Error Description:
error: An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The underlying connection was closed: An unexpected error occurred on a receive.

Solution #1

To resolve the above error,
you can set ‘webRequest.KeepAlive = false;
Generally, this problem is created when the server or another device on the network suddenly closes a
present TCP (Transmission Control Protocol) link.
This kind of issue may take place when a time-out value is set too low on the server or connected
network device. To resolve this issue, see resolutions A, D, E, F, and O. This issue can also occur if the server makes the connection reset unpredictably, such as if an uncontrolled exception crashes the
server function. You should examine the server logs to find if this may be the problem.


Solution #2

Apart from the web Request method, to resolve the above error, you can also set
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
OR
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
OR
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;

OR you can replace Tls12 with Tls11 or Tls13.

About ServicePointManager (SPM) Class

It belongs to Namespace: System.Net and inherited from Assemblies such as netstandard.dll,
System.dll, System.Net.Service Point.dll.
Service Point Manager is generally known as the static class that helps to make, manage, and delete
instances or activities of the Service Point class. Mainly, this class is functioning behind the scenes to put a strong control over the connections. When your application creates multiple connections to a single server, the Service Point Manager class removes the overhead of adding a new connection every time you try to connect.

Checking certificate policy

The Service Point Manager also sets the certificate policy for upcoming connections using
‘Check Certificate RevocationList’ property. In many cases, the certificate policy should fetch your
requirements. 

With a Uniform Resource Identifier (Uri)

When an application desires a connection to an Internet resource URI by the Service Point Manager
(SPM) object, the SPM returns instantly a Service Point object which carries connection data or details for the scheme and host detected by the Uri. If there is a current Service Point object for that scheme and host, the SPM object returns the current Service Point object; otherwise, the SPM object generates a new Service Point object. 

Properties

With Service Point Manager, different properties are used for different purposes. Some properties are
Certificate Policy, Check Certificate Revocation List , Default Connection Limit, Dns Refresh Timeout, Enable Dns Round Robin, Encryption Policy, Max Service Point IdleTime, Max Service Points, Reuse Port, Security Protocol, Server Certificate Validation Callback, Use Nagle Algorithm

Methods

  • Equals(Object)
  • Find Service Point(String, IWebProxy)
  • Find Service Point(Uri)
  • Find Service Point(Uri,IWebProxy) 
  • Get Hash Code()
  • GetType()
  • MemberwiseClone()
  • Set Tcp Keep Alive(Boolean, Int32, Int32) 
  • ToString()

Conclusion

Hence, in latest version of ASP.net using different properties and methods of Service Point Manager, we can obtain more benefits and utilities. If your connection error ‘The underlying connection was closed: An unexpected error occurred on a send of web request’ is still not resolved, don’t hesitate to contact us.

Suggested Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

2 comments:

  1. the underlying connection was closed in c# error is accure while i am trying to create http post request.
    i trying to push json data into the url in c#.
    if you have any kind of solution of this problem please share.
    Thanks.

    ReplyDelete
    Replies
    1. You can put json data limited in URL and also check your web.config settings for upload size.

      Delete