通过PowerShell执行SOAP请求

     SOAP的请求在Web Service是无处不在的,像WCF服务和传统ASMX asp.net的web Service。如果要测试SOAP服务是否好用通过web编程来实现就显得太过于复杂了,下面的脚本片段(snippet)将会轻而易举的完成通过powershell测试和调用SOAP服务:

隐藏行号 复制代码 这是一段程序代码。
  1. function Execute-SOAPRequest 
  2. ( 
  3.         [Xml]    $SOAPRequest, 
  4.         [String] $URL 
  5. ) 
  6. { 
  7.         write-host "Sending SOAP Request To Server: $URL" 
  8.         $soapWebRequest = [System.Net.WebRequest]::Create($URL) 
  9.         $soapWebRequest.Headers.Add("SOAPAction","`"http://www.facilities.co.za/valid8service/valid8service/Valid8Address`"")
  10.         $soapWebRequest.ContentType = "text/xml;charset=`"utf-8`"" 
  11.         $soapWebRequest.Accept      = "text/xml" 
  12.         $soapWebRequest.Method      = "POST" 
  13.         
  14.         write-host "Initiating Send." 
  15.         $requestStream = $soapWebRequest.GetRequestStream() 
  16.         $SOAPRequest.Save($requestStream) 
  17.         $requestStream.Close() 
  18.         
  19.         write-host "Send Complete, Waiting For Response." 
  20.         $resp = $soapWebRequest.GetResponse() 
  21.         $responseStream = $resp.GetResponseStream() 
  22.         $soapReader = [System.IO.StreamReader]($responseStream) 
  23.         $ReturnXml = [Xml] $soapReader.ReadToEnd() 
  24.         $responseStream.Close() 
  25.         
  26.         write-host "Response Received."
  27.         return $ReturnXml 
  28. }
  29. $url = 'http://www.facilities.co.za/valid8service/valid8service.asmx'
  30. $soap = [xml]@'
  31. <?xml version="1.0" encoding="utf-8"?>
  32. <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">
  33.   <soap12:Body>
  34.     <Valid8Address xmlns="http://www.facilities.co.za/valid8service/valid8service">
  35.       <ID>string</ID>
  36.       <Address1></Address1>
  37.       <Address2></Address2>
  38.       <Address3></Address3>
  39.       <Address4></Address4>
  40.       <Address5></Address5>
  41.       <Address6></Address6>
  42.       <PostCode></PostCode>
  43.     </Valid8Address>
  44.   </soap12:Body>
  45. </soap12:Envelope>
  46. '@
  47. $ret = Execute-SOAPRequest $soap $url

      在这里得到的$ret变量中存储的是System.Xml.XmlDocument对象,如果需要查看其中的具体内容可以通过Export-Clixml这个cmdlet将其输出到本地文件中查看。

隐藏行号 复制代码 这是一段程序代码。
  1. $ret | Export-Clixml  c:\1.xml;Get-Content c:\1.xml

作者: 付海军

版权:本文版权归作者所有

转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢

要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

个人网站: http://shell.tor.hu/


当前日志信息