Powershell:Invoke-WebRequest

时间:2017-04-03 15:31:42

标签: powershell soap

我使用以下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 param代替我以便我可以丰富内容,但我面临一些问题,下面是代码。

## 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'

我收到以下错误

powershell error

我也试过

$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

enter image description here

1 个答案:

答案 0 :(得分:0)

答案是删除变量中的XML缩进,是否在某处记录了此问题?或者是一种限制。

which python