Search code examples
c#.nethttpwebrequest

The underlying connection was closed. An unexpected error occurred on a send


I am trying to connect to an API. I will preface this and say this is a project we were working on months ago, and it was working, at least in the sense i could reach out and get the data from the API fine. We put the project on hold for a couple months, come back to it, and now the connection to this one API no longer works. In the project we are connecting to two API's and the second API connections work fine still. I have attempted to recreate the request using the same headers in Postman and it works fine. I have had this issue before, and adding the line:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

Solved it for me in the past, but I have that code in the method and it still fails now.

Below is the full method:

var encodedData = System.Convert.ToBase64String(
                                    System.Text.Encoding.GetEncoding("ISO-8859-1")
                                    .GetBytes("XXXXXXXXXXXXXXXXXXX:")
                                ); 

string headerString = "Basic " + encodedData;

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxxxxxxxxxxxxxx);

httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, headerString);
httpWebRequest.KeepAlive = false;    

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)768;   
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

Console.WriteLine("Protocol: " + ServicePointManager.SecurityProtocol);

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
//                           SecurityProtocolType.Tls11 |
//                           SecurityProtocolType.Tls12;

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();   
    
    writelog("Result: " + result);
}

Not sure if this helps, but when i try to analyze the traffic in Fiddler, i can see the request coming out of Postman fine, but when I run the above code I never get any traffic leaving my network. Just a lot of traffic from localhost to /vshub/XXXXXXXXXXX. I have also tried other permutations of the ServicePointManager.SecurityProtocol as you can see from the commented out code, but none have worked.

I am at wits end trying to solve this. Any help would be appreciated! Oh, i am on runtime 4.0 and framework 4.6.

UPDATE: Ran the code on alternate machines using the same runtime and framework and it works fine. It seems more and more likely to be a Firewall issue on my local machine.


Solution

  • I solved it. I tried running the code on other machines with a fresh install of Visual Studio. It worked fine on all other machines. I reinstalled VS on my local machine and the code worked. Not sure what changed to my VS over the last month or two but something clearly got corrupted.