我对curl
和PowerShell有疑问。
我在我的服务器(Windows Server 2008 R2 Enterprise)上安装了git,我从PowerShell git/bin/curl
调用。
$tempFile = [IO.Path]::GetTempFileName() | Rename-Item -NewName { $_ -replace 'tmp$', 'xml' } –PassThru
$soupRequestXML | Set-Content $tempFile -Encoding UTF8
cd $env:temp
$cmd = "C:\Program Files (x86)\git\bin\curl -X POST -H `'Content-type:text/xml;charset:UTF-8`' -d `@" + $tempFile.name + " "+ $soapService
Invoke-Expression $cmd
$soupRequestXML
是我的肥皂要求。
问题是,PowerShell在解析@
字符时遇到一些麻烦。
这是PowerShell错误:
Invoke-Expression:Die Splat-Variable“@ tmpCEA7”kann nicht erweitert werden。 Splat-Variablenkönnenichsals Teil eines Eigenschafts- oder Arrayausdrucks verwendet werden。 Weisen Sie das Ergebnis des AusdruckseinertemporärenSvaruzu,undführenSiestattdessen einen Splat-Vorgangfürdiemportäre变量aus。
抱歉,我知道它是德语版,但我的工作服务器不是我的。就像你可以看到我已经试图逃脱@
角色,但它仍然无法正常工作。
我还尝试将字符串直接传递给curl
:
$cmd = "C:\Program Files (x86)\git\bin\curl -X POST -H `'Content-type:text/xml;charset:UTF-8`' -d `'" + $(Get-Content $tempFile) + "`' "+ $soapService
但是,curl
似乎有一些问题需要解析,所以有人有想法吗?
curl: (6) Could not resolve host: <soapenv curl: (6) Could not resolve host: <soapenv curl: (6) Could not resolve host: <com curl: (6) Could not resolve host: <arg0>xx< curl: (6) Could not resolve host: <arg1>xxx< curl: (6) Could not resolve host: < curl: (6) Could not resolve host: <
这是我的SoapRequest XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com=\"http://host...../">
<soapenv:Header/>
<soapenv:Body>
<com:test>
<arg0>xx/arg0>
<arg1>xx</arg1>
</com:test>
</soapenv:Body>
</soapenv:Envelope>
答案 0 :(得分:1)
XML中双引号的组合以及将Invoke-Expression
与命令字符串结合使用会使事情变得混乱。
首先,当您可以使用呼叫运算符(Inovoke-Expression
)时,请不要使用&
。给你带来更少的逃避头痛。用单引号替换XML字符串中的双引号,以使它们不受影响。
& "C:\Program Files (x86)\git\bin\curl" -X POST `
-H 'Content-type:text/xml;charset:UTF-8' `
-d "$((Get-Content $tempFile) -replace '"',"'")" $soapService
话虽如此,如果你正在使用PowerShell,那么使用Invoke-WebRequest
会更有意义:
[xml]$data = Get-Content $tempFile
$headers = @{'SOAPAction' = '...'}
Invoke-WebRequest -Method POST -Uri $soapService -ContentType 'text/xml;charset="UTF-8"' -Headers $headers -Body $data
或(因为你似乎陷入了PowerShell v2)System.Net.WebRequest
类:
[xml]$data = Get-Content $tempFile
$req = [Net.WebRequest]::Create($soapService)
$req.Headers.Add('SOAPAction', '...')
$req.ContentType = 'text/xml;charset="utf-8"'
$req.Method = 'POST'
$stream = $req.GetRequestStream()
$data.Save($stream)
$stream.Close()
$rsp = $req.GetResponse()
$stream = $rsp.GetResponseStream()
[xml]$result = ([IO.StreamReader]$stream).ReadToEnd()
$stream.Close()