Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "The underlying connection was closed: An unexpected error occurred on a send" on GetReponse() but not via browser

Tags:

vb.net

I am attempting to utilize a REST service that uses a GET method. I'm using the .Net Framework 4.5.2. What I've written below is just a test to see if I can actually make the request. If I put the URL directly into a browser I get a good response back with string data in json format. But when I attempt to run the code I end up getting the following error back:

"The underlying connection was closed: An unexpected error occurred on a send." InnerException = {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}

I have tried setting the keepalive to both true and false and I've tried using a WebClient along with DownloadString too....all result in the same error. Does anyone have any idea of what I'm missing? Or is this a problem somehow on the server side?

Dim script As String = "236"
Dim deploy As String = "1"
Dim compid As String = "915960_SB2"
Dim h As String = "value1"
Dim id As String = "1241389"
Dim status As String = "in freezer"
Dim request As HttpWebRequest
Dim response As HttpWebResponse

Try

'I have removed the real website name from this code.
            Dim theurl As String = "https://website/app/site/hosting/scriptlet.nl?script={0}&deploy={1}&compid={2}&h={3}&Id={4}&Status={5}"
            Dim url As String = String.Format(theurl, script, deploy, compid, h, id, status)

            request = HttpWebRequest.Create(url)
            request.Method = "GET"
            request.ContentType = "application/json"

'This is where the error occurs
            response = request.GetResponse()

Catch ex As Exception
            Dim test as string=""
Finally

End Try 
like image 783
Eric Cooper Avatar asked Sep 05 '25 17:09

Eric Cooper


1 Answers

I solved it. I stumbled upon the following web site where someone else had been having the same issue.

http://www.avivroth.com/2013/05/02/rest-calls-in-net-c-over-ssl-https/

The solution is to set the security protocol. I just went down the list from ssl to tls11 then to tls12 and found that the last one worked.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

like image 86
Eric Cooper Avatar answered Sep 07 '25 17:09

Eric Cooper