2016-04-15 256 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