PowerShell小技巧之执行SOAP请求
2018-10-12 14:10
561 查看
SOAP的请求在Web Service是无处不在的,像WCF服务和传统ASMX asp.net的web Service。如果要测试SOAP服务是否好用通过web编程来实现就显得太过于复杂了,下面的脚本片段(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","`"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将其输出到本地文件中查看。
这是一段程序代码。
$ret | Export-Clixml c:\1.xml;Get-Content c:\1.xml
您可能感兴趣的文章:
相关文章推荐
- 通过PowerShell执行SOAP请求
- 通过PowerShell执行SOAP请求
- 关于PowerShell脚本执行时,提示服务器不愿意处理该请求
- 第二篇:利用shell脚本执行webservice请求——基于soap
- PowerShell小技巧之发送TCP请求
- Powershell小技巧之使用Jint引擎在PowerShell中执行Javascript函数
- PowerShell 执行策略
- 无侵入方面编程-用HttpModule+SoapExtension监视页面执行参数(二)
- Powershell中请求WebServices并以JSON格式输出结果
- Powershell小技巧之复合筛选
- 解决C#操作PowerShell时有时需要确认的操作不能执行问题,附操作VisualSvn权限的方法
- 使用C#执行PowerShell命令
- Powershell小技巧之屏蔽输出结果
- ajax请求返回的数据看不到回调函数没有执行也没报错
- Jmeter发送SOAP请求对WebService接口测试
- lr soap请求webservice—soap_request
- 第一次使用MsChart运行提示:ChartImg.axd 执行子请求时出错。
- SOAP web service用AFNetWorking实现请求
- 解析如何通过PHP函数获取当前运行的环境 来进行判断执行逻辑(小技巧)
- 发送一个SOAP请求后的处理过程-----实例