Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API responds with 'An existing connection was forcibly closed by the remote host'

My company had some dotNET code which has been talking to Twitter's API happily over the last three months, tweeting our news out.

On 29th July it stopped working, and the POST request was hit with this error:
An existing connection was forcibly closed by the remote host
System.Net.Sockets.SocketException

I assume this is because our server is still using TLS 1.1 and I only just found this Twitter API announcement from June, which says:

Since the removal of SSL support 104 in 2014, the Twitter API has required a minimum of TLS 1.0 for all incoming connections. Both TLS 1.0 and 1.1 were superseded by TLS 1.2 in 2008.

Beginning July 25th 2019, all connections to the Twitter API (and all other Twitter domains) will require TLS 1.2. This change will affect all formats and tiers of the API (REST, streaming, and webhooks; standard, premium, enterprise, and Ads and Media APIs), as well as the wider Twitter platform.

It seems we got 4 days grace, as our code had no problems between 25th and 28th July.

I believe the server itself has TLS 1.2 installed, so perhaps the issue is that this project was compiled with dotNET 4.0 which defaults to using TLS 1.1?

Will upgrading our project to dotNET 4.5 solve the problem, or could it be something else?

like image 295
Magnus Smith Avatar asked Jul 30 '19 12:07

Magnus Smith


People also ask

Why An existing connection was forcibly closed by the remote host?

​An existing connection was forcibly closed by the remote host​ This error message may occur when migrating a large item (containing large attachments, for instance). ​The Destination system will keep a connection open only for so long, after which the connection will be closed (timeout).


3 Answers

I came to install dotNET 4.5 and was given the options to repair/remove, indicating it was already installed! This was unexpected.

So I added this line of code:
System.Net.ServicePointManager.SecurityProtocol = 3072

And changed Web.config to use targetFramework="4.5"

Everything now working again. Thanks.

like image 123
Magnus Smith Avatar answered Nov 12 '22 12:11

Magnus Smith


I had the same issue start on July 29th as well. My project is compiled with .NET 4.5.2 and uses the LinqToTwitter library. I was able to fix the problem by adding the following line prior to making Twitter calls.

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
like image 45
Roice Nelson Avatar answered Nov 12 '22 11:11

Roice Nelson


Just had the same issue myself. However, beware that the change to ServicePointManager will apply to all endpoints. So, if you are connecting to another that doesn't support TLS 1.2, it will stop working.

So, a better solution would be to or the Tls12 value with the existing value.

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
like image 20
illusio Avatar answered Nov 12 '22 10:11

illusio