PowerShell小技巧之執(zhí)行SOAP請求
更新時間:2014年10月13日 09:35:37 投稿:hebedich
SOAP(Simple Object Access Protocol )簡單對象訪問協(xié)議是在分散或分布式的環(huán)境中交換信息的簡單的協(xié)議,是一個基于XML的協(xié)議,它包括四個部分:SOAP封裝(envelop),SOAP編碼規(guī)則(encoding rules),SOAP RPC表示(RPC representation,SOAP綁定(binding)
SOAP的請求在Web Service是無處不在的,像WCF服務和傳統(tǒng)ASMX asp.net的web Service。如果要測試SOAP服務是否好用通過web編程來實現(xiàn)就顯得太過于復雜了,下面的腳本片段(snippet)將會輕而易舉的完成通過powershell測試和調用SOAP服務:
這是一段程序代碼。
復制代碼 代碼如下:
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","`" $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
}
$url = 'http://www.facilities.co.za/valid8service/valid8service.asmx'
$soap = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi=" <soap12:Body>
<Valid8Address xmlns=" <ID>string</ID>
<Address1></Address1>
<Address2></Address2>
<Address3></Address3>
<Address4></Address4>
<Address5></Address5>
<Address6></Address6>
<PostCode></PostCode>
</Valid8Address>
</soap12:Body>
</soap12:Envelope>
'@
$ret = Execute-SOAPRequest $soap $url
在這里得到的$ret變量中存儲的是System.Xml.XmlDocument對象,如果需要查看其中的具體內容可以通過Export-Clixml這個cmdlet將其輸出到本地文件中查看。
這是一段程序代碼。
復制代碼 代碼如下:
$ret | Export-Clixml c:\1.xml;Get-Content c:\1.xml
相關文章
PowerShell查看本機文件關聯(lián)程序和默認打開程序的方法
這篇文章主要介紹了PowerShell查看本機文件關聯(lián)程序和默認打開程序的方法,本文給出了查看方法,同時給出了一份讀取結果,需要的朋友可以參考下2015-06-06
PowerShell獲取系統(tǒng)環(huán)境變量的方法
這篇文章主要介紹了PowerShell獲取系統(tǒng)環(huán)境變量的方法,本文講解了列出所有的環(huán)境變量的方法和獲取環(huán)境變量的值的方法,需要的朋友可以參考下2014-08-08

