首页 > 解决方案 > Powershell Invoke-WebRequest 导致服务器错误

问题描述

我有一个 xml 文件,我正在使用 Powershell 脚本创建一个肥皂请求以将其发布到 url。

我似乎遇到的问题是下面这行代码产生了服务器错误:

[xml]$res = Invoke-WebRequest $uri -Method POST -ContentType "text/soap+xml;charset=windows-1252" -Body $soapReq

奇怪的是,当我在记事本中打开我的数据 XML 文件并保存它(不做任何更改)时,Powershell 脚本似乎工作并将文件传输到目标 url。

我正在用 UTF8 字符集编写我的 xml 文件。

我附上了我正在使用的 Powershell 脚本的调整版本。

#my db generates this file
$filePath = "C:\MyPath\data.xml"

$result = Get-Content $filePath

#rewrite the file to a specific location and encode it to utf-8
Out-File -FilePath $filePath -InputObject $result -Encoding 'UTF8'

$un = 'un'
$Pw = 'pw'

$xmlData = Get-Content $filePath -Raw
$xmlData = [Security.SecurityElement]::Escape($xmlData)

$soapReq = @"
<?xml version="1.0" encoding="Windows-1252"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="https://url">
<env:Body>
  <ins0:Transfer>
    <sourceIdentifier>123456</sourceIdentifier>
    <userName>$un</userName>
    <password>$pw</password>
    <xmlDataset>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
$xmlData</xmlDataset>
  </ins0:Transfer>
</env:Body>
</env:Envelope>
"@

$uri = "https://url"

[xml]$res = Invoke-WebRequest $uri -Method POST -ContentType "text/soap+xml;charset=windows-1252" -Body $soapReq

在我调查使用 vbs 在记事本中打开文件并保存它(矫枉过正)之前,我希望有人能给我关于如何让这个 Powershell 脚本正常工作的建议。

标签: powershell

解决方案


推荐阅读