powershell - 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><?xml version="1.0" encoding="UTF-8"?>
$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 脚本正常工作的建议。
解决方案
推荐阅读
- windows - Windows PowerShell 的“rm -rf”等效项?
- linux - tkinter 在 windows 和 linux 下有区别吗?
- react-native - WebXR 在 React Native WebView 组件中不起作用
- angular - Jhipster - 为新警报添加翻译
- reactjs - 如何将数据从 API 保存到 Reactjs 中的状态?
- rxjs - RxJS - 没有订阅/行为主题也一样?
- python - 从熊猫数据框列中的地址中提取部分
- html - 引导容器行列中列表项之间的空格
- maven - 自定义 maven 原型:生成默认为 3.1.1 版本而不是 3.1.2?
- javascript - Fetch API - 仅下载标头