2016-04-15 258 views
0

我有一个关于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> 
+0

为什么使用'curl'而不是'Invoke-WebRequest'? –

+0

因为我是Java开发人员,而且我对PowerShell没有太多经验?我还认为这只适用于Poweshell版本> 2.0? – DaveTwoG

回答

0

的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() 
+0

完美的作品:-)。我已经更改为PowerShell v2中的标准soapRequest。 @Ansgar Wiechers:非常感谢您的帮助 – DaveTwoG