Direct code post data call WebService

Source: Internet
Author: User
Tags soapui

Original: Direct code post data call WebService

PS: children's shoes with webservice probably understand that it is based on the SOAP protocol to exchange data, while the SOAP protocol is an extension of the HTTP protocol, in fact, we can think that invoking a Web service is the process of getting or post data through the HTTP protocol , except that the intermediate input/output data is standard XML that adheres to the SOAP protocol format. Knowing this, we can construct the request data to simulate the process of invoking the Web service, with the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Encoding Encoding=Encoding.UTF8; stringResponseData =String.Empty; WebRequest Request= Httpwebrequest.create ("http://localhost:32100/Service1.svc"); stringparam ="<soapenv:envelope xmlns:soapenv=\ "http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\ "http// Tempuri.org/\ "xmlns:wcf=\" http://schemas.datacontract.org/2004/07/wcfservice2\ ">"+"<soapenv:Header/>"+"<soapenv:Body>"+"<tem:GetDataUsingDataContract>"+"<!--optional:-->"+"<tem:composite>"+"<!--optional:-->"+"<wcf:BoolValue>true</wcf:BoolValue>"+"<!--optional:-->"+"<wcf:StringValue>ddd</wcf:StringValue>"+"</tem:composite>"+"</tem:GetDataUsingDataContract>"+"</soapenv:Body>"+"</soapenv:Envelope>"; byte[] bs =System.Text.ASCIIEncoding.ASCII.GetBytes (param); Request. Method="POST"; Request. ContentType="Text/xml;charset=utf-8"; Request. ContentLength=BS.            Length; //specify to invoke http://the Getdatausingdatacontract method in Localhost:32100/service1.svc service//http://tempuri.org/IService1/GetDataUsingDataContract(Namespace (http://tempuri.org/)/Interface Name (IService1)/method name (getdatausingdatacontract))Request. headers["SOAPAction"] ="http://tempuri.org/IService1/GetDataUsingDataContract"; using(Stream Reqstream =request. GetRequestStream ()) {Reqstream.write (BS,0, BS.                Length);            Reqstream.close (); }            using(HttpWebResponse response =(HttpWebResponse) request. GetResponse ()) {using(StreamReader reader =NewStreamReader (response. GetResponseStream (), encoding)) {ResponseData=Reader. ReadToEnd ().                ToString ();                } console.write (ResponseData);            Console.readkey (); }        }    }}

Of course, the above example is completely hard-coded for the request data, and the actual use has to be adjusted and encapsulated according to the actual situation. Is the result of the call, is a bunch of XML in accordance with the SOAP format, as to how to parse I will not say.

Here is one more thing to mention is how to crawl WebService request information, I am through soapui this tool to obtain. (has not used soapui???? Ask the degree Niang bar ... )

The purpose of writing this article: Java development of the webservice through the. NET to invoke often some inexplicable problems (the two standards are not the same, incompatible) toss for a long time not to solve, so can only out of this.

Direct code post data call WebService

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.