Introduction :
An existing connection was forcibly closed by the remote host. [SocketException (0x2746): An existing connection was forcibly closed by the remote host], This happens with a socket connection between client and server. The connection is alive and well, and heaps of data is being transferred, but it then becomes disconnected out of nowhere.
[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +6416371
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +130
Line 60: request.ContentLength = data.Length;
Line 61:
Line 62: using (var stream = request.GetRequestStream())
Line 63: {
Line 64: stream.Write(data, 0, data.Length);
This happens with a socket connection between client and server. The connection is alive and well, and heaps of data is being transferred, but it then becomes disconnected out of nowhere.
Has anybody seen this before? What could the causes be? I can kind of guess a few causes, but also is there any way to add more into this code to work out what the cause could be?
Solution :
we troubleshooted a “An existing connection was forcibly closed by the remote host:” error message. We made some basic tests, such as a “browser test” and found that the certificate used was not valid. However fixing the certificate did not solve the issue and we still see the same error message telling us that the existing connection is forcibly closed by the remote host when we run our ASP.NET application which makes an HTTP web request to https://test.com/ URL.
when this error come we need to to enable tls in our operating sysytem like below :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols
Also need to add tls enable code in below solution
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://www.example.com/query“);
request.Method = “POST“;
request.ContentType = “application/json“;
string jsonOrder = JsonConvert.SerializeObject(hm);
var data = Encoding.UTF8.GetBytes(jsonOrder);
request.ContentLength = data.Length;using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
if (((HttpWebResponse)response).StatusDescription == “OK“)
{
using (var stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
responseMessage = reader.ReadToEnd();}
}
Also Learn More Tutorial :
Auto Refresh Partial View in ASP.NET MVC
Search Functionality in ASP.NET MVC
Insert, Update, Delete In GridView Using ASP.Net C#
Hover Effects for GridView Rows Using CSSPass session variable from page to page Asp.Net C#
Binding Dropdownlist With Database In Asp.Net MVC
Binding Database Values in Table Using MVC Razor
Select Insert, Update And Delete With ASP.NET MVC
Display Data in GridView Using Asp.net MVC
Validation in ASP.NET MVC Razor view
CRUD Operation Using 3-Tier Architecture In ASP.NET
Binding Dropdownlist With Database In Asp.Net MVC
Asp.Net Image Upload in 3-Tier Architecture and store in sql database
How to calculate time duration between Logintime and Logouttime in asp.net c#
Search records in GridView using asp.net c#
Query string in asp.net c# with example
How to get Connection String from Web.Config in Asp.Net C#