Underlying Connection was closed

Hello.

I’m getting an when calling the API through my application

System.Net.WebException: The Underlying Connection was closed: An unexpected error occurred on a send.
Systtem.IO.IOException Authenication failed because the remote party has closed the transport stream

When called through curl it works fine, and when the application fires curl it works fine, but a httpWebRequest via the application fails.

Any advice appreciated.

Failing authorisation due to incorrect formatting and encoding of the authorisation header can cause that exception. Check if this helps: Getting invalid authorization - #7 by csmith

Another possibility is the security protocal being used by the request. It’s been a while but in my own code I have commented out a couple of lines where I had to explicitly set the protocol because older versions of .NET defaulted to one that was not supported.

You could try playing around with that, check which protocol is being used before the request:

i As Integer = ServicePointManager.SecurityProtocol

And then try changing it to a different one and seeing if that fixes things

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.XXX

I think there might be a minimum level of .NET needed. I’m not an expert and it’s been a while. I had to trial and error it at the time, but now (with a newer version of .NET) it works by default for us.

Sometime in September or October last year we had to add:

System.Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12

before the API call for things to work as before. Hope this works for you.

Fantastic! explicitly setting the SecurityProtocol has done the job. Thanks both.

2 Likes