通过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.  
  11. $soapWebRequest.ContentType = "text/xml;charset=`"utf-8`""
  12. $soapWebRequest.Accept = "text/xml"
  13. $soapWebRequest.Method = "POST"
  14.  
  15. write-host "Initiating Send."
  16. $requestStream = $soapWebRequest.GetRequestStream()
  17. $SOAPRequest.Save($requestStream)
  18. $requestStream.Close()
  19.  
  20. write-host "Send Complete, Waiting For Response."
  21. $resp = $soapWebRequest.GetResponse()
  22. $responseStream = $resp.GetResponseStream()
  23. $soapReader = [System.IO.StreamReader]($responseStream)
  24. $ReturnXml = [Xml] $soapReader.ReadToEnd()
  25. $responseStream.Close()
  26.  
  27. write-host "Response Received."
  28.  
  29. return $ReturnXml
  30. }
  31.  
  32. $url = 'http://www.facilities.co.za/valid8service/valid8service.asmx'
  33. $soap = [xml]@'
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <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">
  36. <soap12:Body>
  37. <Valid8Address xmlns="http://www.facilities.co.za/valid8service/valid8service">
  38. <ID>string</ID>
  39. <Address1></Address1>
  40. <Address2></Address2>
  41. <Address3></Address3>
  42. <Address4></Address4>
  43. <Address5></Address5>
  44. <Address6></Address6>
  45. <PostCode></PostCode>
  46. </Valid8Address>
  47. </soap12:Body>
  48. </soap12:Envelope>
  49. '@
  50. $ret = Execute-SOAPRequest $soap $url

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

$ret | Export-Clixml c:\1.xml;Get-Content c:\1.xml

作者: 付海军
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.shell.tor.hu/

本文出自 “@天行健中国元素” 博客,转载请与作者联系!

你可能感兴趣的:(function,测试,职场,asp.net,休闲)