curl SOAP请求
我有一个关于curl
和PowerShell的问题。curl SOAP请求
我已经在我的服务器(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-Variablen können nicht als Teil eines Eigenschafts- oder Arrayausdrucks verwendet werden. Weisen Sie das Ergebnis des Ausdrucks einer temporären Variable zu, und führen Sie stattdessen einen Splat-Vorgang für die temporäre Variable 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>
的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()
完美的作品:-)。我已经更改为PowerShell v2中的标准soapRequest。 @Ansgar Wiechers:非常感谢您的帮助 – DaveTwoG
为什么使用'curl'而不是'Invoke-WebRequest'? –
因为我是Java开发人员,而且我对PowerShell没有太多经验?我还认为这只适用于Poweshell版本> 2.0? – DaveTwoG