How to call a https wcf service

I have a https website running say https://localhost:1234/HelloService/ . I have to write a powersheel script which will get connected to this website and consume the data. I could do using http following some tutorials, but i am unable to do it for https.

Please share some example code and error messages.

add-type @"
   using System.Net;
   using System.Security.Cryptography.X509Certificates;
   public class TrustAllCertsPolicy : ICertificatePolicy 
    {
      public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem)        
        {
           return true;
        }
    }
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$result = Invoke-WebRequest -Uri "https://localhost:1234/HelloService/ "

Error :

Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
At D:\Untitled1.ps1:15 char:11

  • $result = Invoke-WebRequest -Uri "https://localhost:1234/HelloService/
  •       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Can you browse to the URL using HTTPS without getting any errors? That’s a fairly generic error but I would suspect a problem with either the certificate or the webserver.

Edit: Just had a thought, won’t you always get a certificate error if you go to localhost?