【发布时间】:2017-04-04 00:00:32
【问题描述】:
我正在使用以下 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
【问题讨论】:
-
您能分享一下您在 infile 中传递的内容吗?
-
@RanadipDutta 本质上,与 $body 变量中定义的内容相同,我刚刚删除了机密细节,一个简单的 xml SOAP
-
很好。让我来看看它,然后在上面创建一个示例。
-
@RanadipDutta 我刚刚发现这是 xml 缩进,由于某种原因 powershell 不接受它。我已经删除了所有的空格,它工作正常。
标签: powershell soap