1

PowerShell 很新,希望有人能指出我正确的方向。

我需要执行 POST 请求,并且必须在 POST 请求期间将本地存储的证书(x509)传递给它,以进行身份​​验证。

实现这一目标的最佳方式或方法是什么?我找到了很多能够在 .net/C# 中执行此任务的示例,但我没有找到任何可以在 PowerShell 中完成此任务的东西。

这是我的 POST 请求代码,我想再次指向本地存储的证书“C:\code\cert.crt”并在网络事务期间传递它。

$url = "https://myUrl/uploadTester"
$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-www-form-urlencoded"
$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()

感谢所有高级帮助。

4

1 回答 1

0

将 C# 转换为 PowerShell 非常容易。试试这个:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile("C:\Users\Andy\Desktop\Test.cer")
$web = [System.Net.WebRequest]::Create($url)
$web.ClientCertificates.Add($Cert)

我改编自:http: //support.microsoft.com/kb/895971

看起来关键是ClientCertificates属性。

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.clientcertificates.aspx

于 2012-04-12T02:35:56.740 回答