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

北战南征 提交于 2020-01-22 13:49:42

问题


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?


回答1:


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;



回答2:


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;



回答3:


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.




回答4:


Same issue. The call was failing when Twitter was calling ExternalLogin. I assume it was a TLS 1.2 issue. It was failing here (see code below) as the User was not authenticated. Facebook Auth would not trigger a new ChallengeResult, so I knew this was how it was supposed to work.

if (!User.Identity.IsAuthenticated)
{
  return new ChallengeResult(provider, this);
} 

My Asp.Net Web API was already targeting 4.6.1, but the runtime was configured as 4.5. The fix for me was to update this line in the Web.config and rebuild the app.

<system.Web>
<httpRuntime targetFramework="4.6.1" />
</system.Web>

Once I made this change, the Twitter Auth window displayed once again.

NOTE: I've read elsewhere that you should not mess with the System.Net.ServicePointManager.SecurityProtocol setting. Asp.Net 4.6.1 defaults to TLS 1.2, but it is supposed to be backwards compatible with 1.1 and 1.0. I'm about to test with some Xamarin apps that use this API, so I will let everyone know what happens. Update coming!



来源:https://stackoverflow.com/questions/57271345/twitter-api-responds-with-an-existing-connection-was-forcibly-closed-by-the-rem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!