Powershell Invoke-RestMethod传递xml主体时出错

时间:2016-10-21 19:25:06

标签: xml rest powershell

我正在尝试编写一个调用休息Web服务的Powershell脚本。下面的代码是我必须进行的最基本的调用,用于查询api并返回特定设备的信息。运行这个相同的脚本我得到不一致的结果,有时它工作正常,有时它会返回此错误:

  

Invoke-RestMethod:在调用[Begin] GetResponse之前,必须将ContentLength字节写入请求流。   在E:\ temp \ temp.ps1:28 char:18   + $ searchResults = Invoke-RestMethod -Method Post -Uri $ searchURI -Credential $ cr ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:NotSpecified:(:) [Invoke-RestMethod],ProtocolViolationException       + FullyQualifiedErrorId:System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodComm      和

我无法弄清楚这个错误意味着什么或它为何间歇性地工作。当Powershell脚本因该错误而失败时,我已经对Postman的目标Web服务进行了测试,并确认Web服务正常运行。

#Get PS Credential Object
$user = "user"
$pass = "password"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($user, $secpasswd)

$searchURI = "http://internal.server.com:8080/spectrum/restful/models"

$searchBody = @"
<?xml version="1.0" encoding="UTF-8"?>
<rs:model-request xmlns:rs="http://www.ca.com/spectrum/restful/schema/request" throttlesize="250000">
<rs:target-models><rs:models-search><rs:search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
<devices-only-search />
<filtered-models>
<has-substring-ignore-case>
<attribute id="0x1006e">
<value>SERVER1234</value>
</attribute>
</has-substring-ignore-case>
</filtered-models>
</rs:search-criteria>
</rs:models-search>
</rs:target-models>
</rs:model-request>
"@

$searchResults = Invoke-RestMethod -Method Post -Uri $searchURI -Credential $cred -Body $searchBody -ContentType "application/xml"
write-host $searchResults.OuterXml

0 个答案:

没有答案