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!
Examples:
Baidu Network disk: Http://pan.baidu.com/s/1hquuXHa
csdn:http://download.csdn.net/detail/hater22/7490147