PowerShell Implementation SOAP Request _powershell

Source: Internet
Author: User
Tags soap xmlns

Soap requests are ubiquitous in Web services, such as the WCF service and the traditional ASMX asp.net Web service. If it is too complicated to test whether the SOAP service is easy to implement through Web programming, the following script fragment (snippet) will easily complete the PowerShell test and invoke the SOAP service:

This is a piece of program code.

Copy Code code as follows:

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

The System.Xml.XmlDocument object is stored in the $ret variable here, and if you need to see what's in it, you can output it to a local file by Export-clixml this cmdlet.

This is a piece of program code.

Copy Code code as follows:

$ret | Export-clixml C:\1.xml; Get-content C:\1.xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.