C # implement SOAP to call WebService

Source: Internet
Author: User

I recently wrote an SOA service and started to think that someone else can get my service address and then add a reference directly to use it. The result "Daniel" tells me that it cannot be used.

Let me write a SOAP call service example. I am a little confused, because I have never done this, so I got a Demo and learned it.

 

Learn the following:

There is an object in. Net: WebRequest, which can directly request the service method in the background.

Step 1

var webRequest = (HttpWebRequest)WebRequest.Create(this.Uri);webRequest.Headers.Add("SOAPAction", String.Format("\"{0}\"", this.SoapAction));webRequest.ContentType = "text/xml;charset=\"utf-8\"";webRequest.Accept = "text/xml";webRequest.Method = "POST";webRequest.Credentials = this.Credentials;

A:In the above Code, there is a SOAPAction, which is to access the service after you deploy the service in IIS, such:

The figure tells the user: SOAPAction: "http://tempuri.org/ProcessFlowRequest"

B:WebRequest. Credentials = this. Credentials;

Is the credential for calling the service

Step 2

After reading the preceding information, You Need To splice the SOAP information in the xml of the soap request.

<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>  <soap:Body>    <{0} xmlns='{1}'>{2}</{0}>  </soap:Body></soap:Envelope>

Replace the corresponding information in the image with the position {X}, and the information is spliced!


Step 3

Var webRequest = (HttpWebRequest) WebRequest. create (this. uri); webRequest. headers. add ("SOAPAction", String. format ("\" {0} \ "", this. soapAction); webRequest. contentType = "text/xml; charset = \" UTF-8 \ ""; webRequest. accept = "text/xml"; webRequest. method = "POST"; webRequest. credentials = this. credentials; // write the request SOAP information using (var requestStream = webRequest. getRequestStream () {using (var textWriter = new StreamWriter (requestStream) {var envelope = SoapHelper. makeEnvelope (this. soapAction, this. arguments. toArray () ;}/// get the SOAP request and return the return webRequest. getResponse ();

This will get the XML returned by the request!

 

It is actually easy to know when it is used!

 

Example:

Baidu Network Disk: http://pan.baidu.com/s/1hquuXHa

Http://download.csdn.net/detail/hater22/7490147 (CSDN)

 

 

 

 

Related Article

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.