我有基本的authentatication使用REST API捲曲工作:PowerShell的HTTP POST REST API基本驗證
curl -X POST -H 'Accept: application/json' -u user:password http://localhost/test/
但是,當我嘗試做使用PowerShell的WebRequest一樣,我得到403(拒絕)。 當我在REST代碼中禁用身份驗證檢查時,此腳本正常工作。
什麼是PowerShell來傳遞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
謝謝!這爲我做了。 – 2012-01-20 03:14:45
您可以使用http://www.base64encode.org/獲取UTF-8 Base64字符串。 – SeriousM 2013-04-03 09:52:35