wolfgang ziegler


„make stuff and blog about it“

Curl, PowerShell, Certificates and Proxies

July 13, 2015

Recently, I was working on a web application and tested it using the PowerShell curl command. All nice and smooth …

curl http ok

Except for when I was using https

curl https nok

The reason was the self-signed certificate I had been using for test purposes, which did not pass the validation check.

Turning Off Certificate Validation

My workaround was to switch to .NET and PowerShell scripting in lieu of the built-in curl command. That way I could:

  1. Turn off certificate validation.
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
  2. Create a new WebClient object.
    $web = New-Object Net.WebClient
  3. Perform a DownloadString call to the HTTPS resource.
    $web.DownloadString(HTTPS_URL)

webclient turn off validation

What about Proxy Servers?

Another requirement of my (test) use case was to involve a proxy server. Therefore I had to adapt my custom PowerShell curl script even further.

  1. I needed an instance of a WebProxy object:
    $proxy = New-Object Net.WebProxy
  2. This new proxy objected needed some configuration:
    $proxy.Address = New-Object Uri(“PROXY_URL_OR_IP”)
    $proxy.BypassProxyOnLocal = $false
    $proxy.UseDefaultCredentials = $false
    $cred = New-Object Net.NetworkCredential("USER", "PASS")
    $proxy.Credentials = $cred
  3. Then, the proxy object was added to the formerly created WebClient instance
    $web.Proxy = $proxy
  4. And the next call to DownloadString was going through the proxy.

    webclient proxy

Here’s the full code again. Hope you find this useful!


[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$web = New-Object Net.WebClient
$proxy = New-Object Net.WebProxy
$proxy.Address = New-Object Uri("PROXY_URL_OR_IP")
$proxy.BypassProxyOnLocal = $false
$proxy.UseDefaultCredentials = $false
$cred = New-Object Net.NetworkCredential("USER", "PASS")
$proxy.Credentials = $cred
$web.Proxy = $proxy
$web.DownloadString("CURL_URL")