"Reprint" C # HttpWebRequest send soap XML

Source: Internet
Author: User
Tags form post

Several ways to call WebService:

Method One: Add a Web reference (simple/convenient limitation client is. NET)

Method Two: Post XML (highlighted in this article)

Method Three: Use the Microsoft MSXML2 component (as if it is not supported on window server2008)

Method Four: Add the form post to the server on the page

//Create HttpWebRequest instances, using WebRequest.CreateHttpWebRequest webRequest = (HttpWebRequest) webrequest.create ("Service Address");//Send RequestWebrequest.method ="POST";//CodingWebrequest.contenttype ="Text/xml";stringSoap ="<?xml version=\ "1.0\" encoding=\ "utf-8\"?>"; Soap+="<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+="<soap:Header>"; Soap+="<soapheader xmlns=\ "http://www.52taiqiu.com/\" >"; Soap+="<Login>"+ Txtuser.text +"</Login>"; Soap+="<Password>"+ Txtpwd.text +"</Password>"; Soap+="</SoapHeader>"; Soap+="</soap:Header>"; Soap+="<soap:Body>"; Soap+="<request xmlns=\ "http://www.52taiqiu.com/\" >"; Soap+="<requestxml><! [Cdata["+ Txtrequest.text +"]]></requestxml>"; Soap+="</Request>"; Soap+="</soap:Body>"; Soap+="</soap:Envelope>"; webrequest.headers["SOAPAction"] ="http://www.52taiqiu.com/Request";//character to Bytebyte[] bytes =Encoding.UTF8.GetBytes (SOAP); Stream writer=Webrequest.getrequeststream (); writer. Write (Bytes,0, Bytes. Length); writer. Flush (); writer. Close ();stringresult ="";//back to HttpWebResponseTry{HttpWebResponse Hwres= Webrequest.getresponse () asHttpWebResponse;if(Hwres.statuscode = =System.Net.HttpStatusCode.OK) {//whether to return successStream Rstream =Hwres.getresponsestream ();//Stream ReadStreamReader sr =NewStreamReader (Rstream, Encoding.UTF8); result=Sr. ReadToEnd (); Sr. Close (); Rstream.close ();}Else{result="Connection Error";}//Closehwres.close (); Txtresponse.text=result;}Catch(System.Net.WebException ex) {String responsefromserver= ex. Message.tostring () +" ";if(ex. Response! =NULL){using(WebResponse response =Ex. Response) {Stream data=Response. GetResponseStream ();using(StreamReader reader =NewStreamReader (data)) {Responsefromserver+=Reader. ReadToEnd ();}}} Txtresponse.text=Responsefromserver;}

"Reprint" C # HttpWebRequest send soap 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.