Curl, PowerShell, Certificates and Proxies
July 13, 2015Recently, I was working on a web application and tested it using the PowerShell curl command. All nice and smooth …
Except for when I was using https …
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:
- Turn off certificate validation.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } - Create a new WebClient object.
$web = New-Object Net.WebClient - Perform a DownloadString call to the HTTPS resource.
$web.DownloadString(HTTPS_URL)
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.
- I needed an instance of a WebProxy object:
$proxy = New-Object Net.WebProxy - 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 - Then, the proxy object was added to the formerly created WebClient instance
$web.Proxy = $proxy - And the next call to DownloadString was going through the 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")