处理来自powershell的SOAP请求

时间:2016-02-19 05:05:00

标签: powershell soap

我尝试使用代码示例。但我不公平。任何人都可以收到错误吗? (我了解New-WebServiceProxy cmdlet。)

function Execute-SOAPRequest 
( 
     [Xml]    $SOAPRequest, 
        [String] $URL 
) 
{ 
        write-host “Sending SOAP Request To Server: $URL” 
        $soapWebRequest = [System.Net.WebRequest]::Create($URL) 
        $soapWebRequest.Headers.Add("SOAPAction" ,"http://www.webservicex.net/globalweather.asmx?WSDL/GetWeather") 

    $soapWebRequest.ContentType = 'text/xml;charset="utf-8"' 
    $soapWebRequest.Accept      = “text/xml” 
    $soapWebRequest.Method      = “POST” 

    write-host “Initiating Send.” 
    $requestStream = $soapWebRequest.GetRequestStream() 
    $SOAPRequest.Save($requestStream) 
    $requestStream.Close() 

    write-host “Send Complete, Waiting For Response.” 
    $resp = $soapWebRequest.GetResponse() 
    $responseStream = $resp.GetResponseStream() 
    $soapReader = [System.IO.StreamReader]($responseStream) 
    $ReturnXml = [Xml] $soapReader.ReadToEnd() 
    $responseStream.Close() 

    write-host “Response Received.” 

      return $ReturnXml 
}

function Execute-SOAPRequestFromFile 
( 
        [String] $SOAPRequestFile, 
        [String] $URL 
) 
{ 
        write-host “Reading and converting file to XmlDocument: $SOAPRequestFile” 
        $SOAPRequest = [Xml](Get-Content $SOAPRequestFile)


    return $(Execute-SOAPRequest $SOAPRequest $URL) 
}

$x = Execute-SOAPRequestFromFile  -SOAPRequestFile soap-W.txt -URL "http://www.webservicex.net/globalweather.asmx?WSDL"

文件soap-W.txt:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.webserviceX.NET" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
<SOAP-ENV:Body>
<tns:GetWeather xmlns:tns="http://www.webserviceX.NET">
<tns:CityName>Tampa</tns:CityName>
<tns:CountryName>United States</tns:CountryName>
</tns:GetWeather>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

服务器返回错误500.通过New-WebServiceProxy进行查询是成功的。

1 个答案:

答案 0 :(得分:8)

您应该尝试New-WebServiceProxy CmdLet。

$svc = New-WebServiceProxy -Uri "http://www.webservicex.net/globalweather.asmx?WSDL"
$svc.GetWeather("Aurillac", "France")