2012-07-23 94 views
3

我正在尝试测试一个非常简单的powershell脚本来从内部售票站点获取/发布数据。我遇到了一个似乎与所需的SSL证书有关的问题。有人可以帮助我了解我需要添加哪些代码才能完成这项工作?Powershell HTTPS REST api GET/POST

由于

返回错误:异常调用“的GetResponse”与“0”的参数(一个或多个):“基础连接已关闭:无法建立用于SSL/TLS安全通道的信任关系。”

目前代码:

$url = "https://username:[email protected]/ticket" 
$command = get-content jsonfile.json 

$bytes = [System.Text.Encoding]::ASCII.GetBytes($command) 
$web = [System.Net.WebRequest]::Create($url) 
$web.Method = "POST" 
$web.ContentLength = $bytes.Length 
$web.ContentType = "application/json" 
$stream = $web.GetRequestStream() 
$stream.Write($bytes,0,$bytes.Length) 
$stream.close() 

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

看看这是否有用http://stackoverflow.com/questions/9917875/power-shell-web-scraping-ssl-tsl-issue/9918045#9918045 – 2012-07-24 00:21:46

+1

谢谢你的帮助。但是,我很快发现Powershell v3现在有一个Invoke-RestMethod cmdlet,它可以满足我的需求。 – floyd 2012-07-25 23:27:23

回答

5

我能得到我需要出去在PowerShell中v3中引入新的Invoke-RestMethod cmdlet的功能。这种方法允许你包含一个证书,我没有收到任何错误。