0

I have a .Net 4.5.2 WebApp that is calling my API. When I point my web app to the LocalHost version of my API, it gets the data, and comes back just fine. I published that API, and confirm that the API is working correctly with PostMan. Then I run the exact same WebApp code, changing only the URI from localhost to live api, and I get a multiple exception error consisting of the following:

  1. An existing connection was forcibly closed by the remote host
  2. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
  3. The underlying connection was closed: An unexpected error occurred on a send.
  4. An error occurred while sending the request.

Here's my calling code

 using (HttpClient client = new HttpClient())
 {
     client.DefaultRequestHeaders.Accept.Clear();
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     client.DefaultRequestHeaders.Add("user", serializedUser);
     response = null;
     try
     {
           //Uri uri = new Uri("https://jsonplaceholder.typicode.com/posts/1");//https works
           Uri uri = new Uri("https://api.acme.com/values/test");
           //Uri uri = new Uri("http://localhost/5000/values/test"); //http localhost works
           response = client.GetAsync(uri).Result; 
      }
      catch (Exception e)
      {
          string er = e.Message;
      }
 }  

EDIT 1: I created a .NET Core app from scratch, and my original code works perfectly calling my live API. My original code also work in .NET 4.5.2 calling a different "https" API.

EDIT 2: So this is where I'm at now, I have created two generic apps from VS 2015, one is a .NET Core Web App, the other a .NET Framework Web App. I have used the above code exactly the same in both apps to call the API. In both apps, I can call a generic "https" api I found online (jsonplaceholder). I can also call the localhost version of my app at "http" from both. In the .NET Core version of the app, I can call my "https" live API and get the results I'm looking for. In the .NET Framework app I still get the same errors.

I can't figure out what the difference is between my Core and Framework requests that is getting one shut down when the other isn't.

Steve Eggering
  • 759
  • 2
  • 9
  • 23
  • Not my area, but I suspect that it has to do with you changing from http to https. See if this post helps: [Make Https call using HttpClient](http://stackoverflow.com/questions/22251689/make-https-call-using-httpclient). – TnTinMn Apr 13 '17 at 23:03
  • You should never post screenshots of code, you should put the code in your question then format it using the `{ }` in the editor of the website. This allows for better searching for future visitors. – Scott Chamberlain Apr 17 '17 at 15:39
  • @ScottChamberlain fixed it. – Steve Eggering Apr 17 '17 at 15:51

2 Answers2

0

It seems you are hosting the application on secured http environment (https). Are you using SSL certificate on the server where you are hosting your Web API? If not, It might be throwing the certificate related exceptions.

Just add the following line before the call to GetAsync and This will ignore the SSL errors.

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

This is only recommended in an intranet environment or other closed network where server identities can't be forged.

C# Ignore certificate errors?

Community
  • 1
  • 1
Charan Ghate
  • 1,384
  • 15
  • 32
  • This fix did not work for me, I tried the one in your post, and the servicepointmanager solution from the link you added. Neither made any difference. – Steve Eggering Apr 17 '17 at 16:22
0

Adding the following line before my API call fixed the issue, but I'd love to hear an explanation of what this line does, and any security risks this might impose using it in my web app.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Props to this answer!

Community
  • 1
  • 1
Steve Eggering
  • 759
  • 2
  • 9
  • 23