2

I'm trying to test an API call on my local machine, using RestSharp, with the following code...

        var client = new RestClient("https://[API URL]");

        var request = new RestRequest( Method.POST);
        request.AddParameter("session", this, ParameterType.RequestBody);

        IRestResponse<SessionOut> response = client.Execute<SessionOut>(request);
        return response.Data.session.id;

In response I get the an error telling me that the request was aborted because it "could not create SSL/TLS secure channel".

Does this mean I need to try and set up https://localhost instead of http://localhost in order to call APIs at https:// addresses?

UPDATE

I have updated my code to the following, as per @Shai_Aharoni's answer below. I am still getting the same error however.

        string pathToYourClientCert = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "[my certificate file");
        var client = new RestClient("[API URL]/");
        client.ClientCertificates = new X509CertificateCollection();
        client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));

        var request = new RestRequest( Method.POST);
        request.AddParameter("session", this, ParameterType.RequestBody);

        IRestResponse<SessionOut> response2 = client.Execute<SessionOut>(request);
clayRay
  • 683
  • 1
  • 14
  • 32
  • Is your system have some kind of proxy, https://stackoverflow.com/a/47675172/713789 – Anirudha Gupta Oct 10 '18 at 06:34
  • No @AnirudhaGupta , just testing straight from localhost to the API. – clayRay Oct 11 '18 at 05:00
  • Check @Shai's answer, if it doesn't work well, can you test it in Postman and HttpClient, If it show same, you need to have installed that certificate in your machine. that answer should work. – Anirudha Gupta Oct 11 '18 at 05:07

3 Answers3

6

Try adding this to your code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Related resources:

Dessus
  • 2,147
  • 1
  • 14
  • 24
Spinbgks
  • 61
  • 1
  • if you cant find SecurityProtocolType.Tls12 and only have SecurityProtocolType.Tls. consider upgrading from .Net 4.0 to 4.5. or just use https://stackoverflow.com/questions/47269609/system-net-securityprotocoltype-tls12-definition-not-found – SoliQuiD Sep 06 '22 at 12:51
1

Well... There are a few steps that you need to complete before you can call you HTTPS endpoint.

1) Make sure that your server supports an HTTPS endpoint (i.e : that the URL https://[APIURL] is reachable.

2) Have a valid server (the api server) certificate installed on the machine that executes the HTTPS call.

3) Add the certificate to your RestSharp client. Similar to something like this:

string pathToYourClientCert = "cer/cert.cer";
client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));

Hope this helps...

Shai Aharoni
  • 1,955
  • 13
  • 25
  • Thanks @Shai_Aharoni, I've added the certificate loading code in, still getting the same error. The certificate was created in IIS and exported using mmc.exe. The website runs (with warning because of self-created certificate) at https:// See my updated answer. – clayRay Oct 14 '18 at 22:01
1

Follow these steps: https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=macos

Also add this to your code in MainActivity.cs -> OnCreate -> Before you load your app:

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

I had to do both for my API requests to work in Xamarin Forms!

Make sure your CERTIFICATE is not SELF-SIGNED!!!

Frosty
  • 39
  • 1
  • 7