Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a self-signed cert

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello all,

Pretty new to PowerShell and hoping someone can point me in the right direction.

I need to perform a POST request and have to pass it a locally stored cert (x509) during the POST request, for authentication.

What is the best way or way to accomplish this? I've found plenty of example to be able to perform this task in .net/C# but I am not finding anything that will accomplish this task in PowerShell.

Here is my POST request code, again I would like to point to a cert stored locally "C:\code\cert.crt" and pass it during the web transaction.

Code:
$url = "[URL unfurl="true"]https://myUrl/uploadTester"[/URL]
$data = '{"data": "988309487577839444"}'
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$b = [System.Text.Encoding]::ASCII.GetBytes($data)
$web = [System.Net.WebRequest]::Create($url)
$web.Method = "POST"
$web.ContentLength = $b.Length
$web.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]
$stream = $web.GetRequestStream()
$stream.Write($b,0,$b.Length)
$stream.close()

$reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
$reader.ReadToEnd()
$reader.Close()

Thanks for all the help in advanced.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top