温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

通过PowerShell执行SOAP请求

发布时间:2020-06-27 13:53:16 来源:网络 阅读:418 作者:fuhj02 栏目:编程语言

     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/

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI