Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host
This error usually means that the target machine is running, but the service that you're trying to connect to is not available. (Either it stopped, crashed, or is busy with another request.)
In English:
The connection to the machine (remote host/server/PC that the service runs at) was made but since the service was not available on that machine, the machine didn't know what to do with the request.
If the connection to the machine was not available, you'd see a different error. I forget what it is, but it's along the lines of "Service Unreachable" or "Unavailable".
Edit - added
It IS possible that this is being caused by a firewall blocking the port, but given that you say it's intermittent ("sometimes when the client tries to connect"), that's very unlikely. I didn't include that originally because I had ruled it out mentally before replying.
Unable to read data from the transport connection:An existing connection was forcibly closed by the remote host
As of .NET Framework 4.0 the default security protocol is TLS 1.0
and SSL 3.0
.
In your application you may need to enable either TLS 1.1
and/or TLS 1.2
.
System.Net.ServicePointManager.SecurityProtocol |=
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using (WebClient webClient = new WebClient())
{
string str = webClient.DownloadString("https://www.morizon.pl/");
}
More details in this stackoverflow post.
Unable to read data from the transport connection: An existing connection was (NOT FORCIBLY) closed on a RESTful Web Service call
I had a similar problem with the use of a shared HttpClient
connecting to a server for REST calls. The problem ended up being a mismatch between the KeepAlive
timeout on the client and server. The client side timeout is set by the MaxServicePointIdleTime
setting on the ServicePointManager
and defaults to 100s. The server side idle timeout was set to a shorter value in our server.
Having a shorter timeout on the server as compared to the client resulted in the server sporadically closing a connection just when the client was attempting to connect. This resulted in the reported exception.
Note that I ultimately found the problem because I also received this exception under the same conditions:
System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
Although the problem could be fixed by using a new HttpClient
for each connection to the server, this goes against Microsoft's recommendation for sharing the HttpClient
. I'd suggest putting the MaxServerPointIdleTime
to a shorter value, perhaps 15 seconds, to see if that addresses the issue. Alternately, if you have access to the server code, you could find its timeout, and attempt to set it to a value longer then the 100s timeout defaulted to on the client.
Related Topics
Visual Studio Publish Project into One Simple Installer
What's the Difference Between the 'Ref' and 'Out' Keywords
Show/Hide the Console Window of a C# Console Application
How to Add a New Row to Datagridview Programmatically
Random.Next Returns Always the Same Values
Replacing .Net Webbrowser Control With a Better Browser, Like Chrome
Difference Between Covariance & Contra-Variance
Make a Borderless Form Movable
Get the Correct Week Number of a Given Date
Reading an Integer from User Input
Given a Filesystem Path, Is There a Shorter Way to Extract the Filename Without Its Extension
How to Cast Object of Type 'System.Dbnull' to Type 'System.String'
How to Split CSV Whose Columns May Contain ,
A Generic Error Occurred in Gdi+, Jpeg Image to Memorystream