[英]Powershell: Invoke-WebRequest
我正在使用以下Powershell腳本將存儲在文本文件(soap.txt)中的xml數據發送到端點。
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("charset", 'utf-8')
$headers.Add("SOAPAction", 'nms:rtEvent#PushEvent')
Invoke-WebRequest http://localhost:8080/nl/jsp/soaprouter.jsp -Method Post -ContentType 'text/xml' -Headers $headers -InFile D:\Scripts\archive\soap.txt
雖然上述方法可以正常工作,但我希望改為使用-Body參數,以便我可以豐富內容,但是我遇到了一些問題,下面是代碼。
## Set SOAP headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("charset", 'utf-8')
$headers.Add("SOAPAction", 'nms:rtEvent#PushEvent')
$url = "http://localhost:8080/nl/jsp/soaprouter.jsp"
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...etc etc
</soapenv:Body>
</soapenv:Envelope>'
Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ContentType 'application/xml'
我收到以下錯誤
我也嘗試過
$body = @"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...rest of code here asd
</soapenv:Body>
</soapenv:Envelope>
"@
Invoke-WebRequest -Method Post -Uri $url -ContentType 'text/xml' -Headers $headers -Body $body
而且我知道必須使用適當的屬性或方法來修改標題,因此我也將內容類型附加到標題中,但這也不起作用,我使用的是Powershell 3.0
答案是刪除變量中的XML縮進,此問題是否記錄在某處? 還是一個限制。
$body = @"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...rest of code here asd
</soapenv:Body>
</soapenv:Envelope>
"@
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.