【发布时间】:2012-02-13 17:16:59
【问题描述】:
我使用 curl 使用 REST API 进行了基本身份验证:
curl -X POST -H 'Accept: application/json' -u user:password http://localhost/test/
但是,当我尝试对 powershell webRequest 执行相同操作时,我得到 403(权限被拒绝)。 当我在 REST 代码中禁用身份验证检查时,此脚本工作正常。
powershell 中在类似于 curl 的 POST 请求中传递凭据的最佳方式是什么,或者我可以做些什么来修复以下脚本。
非常感谢您对此提供一些指导。谢谢。
这是我的 powershell 脚本:
function Execute-HTTPPostCommand() {
param(
[string] $target = $null
)
$username = "user"
$password = "pass"
$webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
$webrequest.ContentLength = $PostStr.Length
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password
$webRequest.PreAuthenticate = $true
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)
v$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
$post = "volume=6001F930010310000195000200000000&arrayendpoint=2000001F930010A4&hostendpoint=100000051ED4469C&lun=2"
$URL = "http://example.com/test/"
Execute-HTTPPostCommand $URL
【问题讨论】:
标签: rest powershell