2017-02-22 3 views
0

여기 파워 쉘 버전 2 (사용 .NET 클래스 System.Net.HttpWebRequest) 내 REST 호출 (PUT)나머지 호출

통화가 갖는다 본문은 XML 파일이며 PowerShell 버전 3 이상에서 "Body"속성을 사용하여 invoke-WebRequest를 사용하여이 작업을 수행 할 수 있습니다. 하지만 우리는 powershell 버전 2 시스템을 가지고 있기 때문에 .net 클래스로이 작업을 수행해야합니다. 다음은

내 코드입니다

$psuri = "$restUri" 
[string]$psdata = Get-Content -path "$myfile.xml" 
[System.Net.HttpWebRequest]$global:ps = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create("$psuri"); 

$psbuffer = [System.Text.Encoding]::UTF8.GetBytes($psdata) 
$ps.ContentLength = $psdata.length 
$ps.ContentType = "application/xml" 
    [System.IO.Stream]$outputStream = [System.IO.Stream]$ps.GetRequestStream() 
    $outputStream.Write($psdata, 0, $psbuffer.Length) 




$ps.method = "PUT" 
$ps.Accept = "application/xml" 
$ps.AllowAutoRedirect = $false 
$ps.KeepAlive = $true 
$ps.CookieContainer = $CookieContainer 
$ps.PreAuthenticate = $true 

     #Try 
     #{ 

      [System.Net.HttpWebResponse]$psresponse = $ps.GetResponse() 

같은 오류 예외가 "0"인수 (들) "GetRequestStream을"전화와 함께 실패합니다. "이 동사 형으로 콘텐츠 바디를 보낼 수 없습니다" 줄에 : 8 자 : 9

답변

0

좋아, 그래서 마침내 알아 냈어. 문제는 REST가 PUT으로 XML 데이터를 찾고 있었고 변환하는 방식이 적절하지 않다는 것이 었습니다. 이것은 콘텐츠를 얻으려면 (원시 데이터로)

$ psdata = Get-Content -path "$ myfile.xml"-Raw