在powershell版本2(.Net)中使用PUT方法调用Rest,并带有内容体
这是我使用powershell版本2(使用.net类System.net.HttpWebRequest)进行的REST调用(PUT) 该调用有一个主体,它是一个XML文件,我可以在powershell版本3及更高版本中使用带有属性“body”的invoke WebRequest来实现这一点。但是我必须用.net类来做这件事,因为我们有一些powershell版本2系统 下面是我的代码在powershell版本2(.Net)中使用PUT方法调用Rest,并带有内容体,.net,rest,powershell-2.0,.net,Rest,Powershell 2.0,这是我使用powershell版本2(使用.net类System.net.HttpWebRequest)进行的REST调用(PUT) 该调用有一个主体,它是一个XML文件,我可以在powershell版本3及更高版本中使用带有属性“body”的invoke WebRequest来实现这一点。但是我必须用.net类来做这件事,因为我们有一些powershell版本2系统 下面是我的代码 $psuri = "$restUri" [string]$psdata = Get-Content -path
$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行char:9好的,因此我最终也解决了这个问题。问题是其余的人都在寻找XML数据,而我转换它的方式并不正确。这就是我必须做的,以获取内容(作为原始数据) $psdata=获取内容-路径“$myfile.xml”-原始