SOAP的請求在Web Service是無處不在的,像WCF服務和傳統ASMX asp.net的web Service。若是要測試SOAP服務是否好用經過web編程來實現就顯得太過於複雜了,下面的腳本片斷(snippet)將會垂手可得的完成經過powershell測試和調用SOAP服務:web
- 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.facilities.co.za/valid8service/valid8service/Valid8Address`"")
-
- $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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <Valid8Address xmlns="http://www.facilities.co.za/valid8service/valid8service">
- <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將其輸出到本地文件中查看。shell
$ret
|
Export-Clixml
c:\1.xml;Get-Content c:\1.xml
做者: 付海軍
出處:http://fuhj02.blog.51cto.com
版權:本文版權歸做者和51cto共有
轉載:歡迎轉載,爲了保存做者的創做熱情,請按要求【轉載】,謝謝
要求:未經做者贊成,必須保留此段聲明;必須在文章中給出原文鏈接;不然必究法律責任
我的網站: http://txj.shell.tor.hu/編程