C # implements SOAP call WebService

Source: Internet
Author: User

Recently wrote a SOA service, began to feel that others get my service address, and then directly add the reference can be used, the result "Daniel" told No.

Let me write a SOAP call service sample, I am a bit stunned, because did not do this, so I got a demo, and then learn the next.

Learn as follows:

There is an object in. NET: WebRequest it can request the service directly in the background method

The first step

varWebRequest = (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, this is when you deploy a good service in IIS, access to services, such as:

The picture tells the user: SOAPAction: "Http://tempuri.org/ProcessFlowRequest"

B:webrequest.credentials = this. Credentials;

Is the credential that invokes the service

Step Two

With this understanding, you need to splice the SOAP message that you see in the XML for the SOAP request.

<?XML version= ' 1.0 ' encoding= ' utf-8 '?><Soap:envelopeXmlns: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 picture with the corresponding location of {X}, the information stitching is complete!


Step Three

            varWebRequest = (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 request soap Information            using(varRequeststream =Webrequest.getrequeststream ()) {                using(varTextWriter =NewStreamWriter (Requeststream)) {                    varEnvelope = Soaphelper.makeenvelope ( This. SOAPAction, This.                Arguments.toarray ()); }            }            //gets the SOAP request returned            returnWebrequest.getresponse ();

This will get the xml! to return the request.

Actually used to know, the original is very simple!

In the description of a usage, when called, the 404 error will be reported, the exception message is thrown: The server failed to recognize the HTTP header SOAPAction

Workaround:

Add an attribute to the WebService class of. NET (that is, the class under the. asmx file) [SoapDocumentService (Routingstyle=soapserviceroutingstyle.requestelement)]

Examples:

Baidu Network disk: Http://pan.baidu.com/s/1hquuXHa

csdn:http://download.csdn.net/detail/hater22/7490147

C # implements SOAP call WebService

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.