Read a few articles on the Internet, SOAP sample layout is not clear, can not immediately start, deliberately write an example to share with you, while recording backup.
Current Environment: VS2013 + WPF
Private voidButton_Click (Objectsender, RoutedEventArgs e) { stringURL ="Http://www.webxml.com.cn/WebServices/WeatherWS.asmx"; stringSoap = Setsoapmessage ();//Constructing SOAP Request Information stringresult =getsoapresource (URL, soap); Txtshow.text= result. Replace (">",">\n"). Replace ("<string>\n","<string>"); } #regionInitiating a SOAP request/// <summary> ///initiating a SOAP request/// </summary> /// <param name= "url" >URL</param> /// <param name= "Datastr" >Data</param> /// <returns></returns> Public Static stringGetsoapresource (stringUrlstringdatastr) { //initiating a requestUri uri =NewUri (URL); WebRequest WebRequest=webrequest.create (URI); Webrequest.contenttype="text/xml; Charset=utf-8"; Webrequest.method="POST"; using(Stream Requeststream =Webrequest.getrequeststream ()) { byte[] Parambytes =Encoding.UTF8.GetBytes (datastr. ToString ()); requestStream.Write (Parambytes,0, parambytes.length); } //ResponseWebResponse WebResponse =Webrequest.getresponse (); using(StreamReader Mystreamreader =NewStreamReader (WebResponse.GetResponseStream (), Encoding.UTF8)) { stringresult =""; returnresult =Mystreamreader.readtoend (); } } #endregion #regionConstructing SOAP Request InformationstringSetsoapmessage () {stringHeader =""; stringBODY =""; stringFault =""; Body="<getregioncountry xmlns=\ "http://webxml.com.cn/\"/>"; returnGetsoapmessagebybase (header, body, fault); } #endregion #regionSOAP message Basic structure/// <summary> ///SOAP Message Basic structure/// </summary> /// <param name= "header" >head (with header)</param> /// <param name= "Body" >content Body (contains body)</param> /// <param name= "Fault" >error hint (contains fault)</param> /// <returns></returns> stringGetsoapmessagebybase (stringHeaderstringBodystringfault) {StringBuilder Soap=NewStringBuilder (); Soap. Append ("<?xml version=\ "1.0\" encoding=\ "utf-8\"?>"); Soap. Append ("<soap:envelope xmlns:soap=\ "http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\ "http://www.w3.org/ 2001/xmlschema\ "xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ ">"); //Add Head if(!string. Isnullorwhitespace (header)) {soap. Append ("<soap:Header>"); Soap. Append (header); Soap. Append ("</soap:Header>"); } //Add Content if(!string. Isnullorwhitespace (body)) {soap. Append ("<soap:Body>"); Soap. Append (body); //Add error if(!string. Isnullorwhitespace (fault)) {soap. Append ("<soap:Fault>"); Soap. Append (fault); Soap. Append ("</soap:Fault>"); } soap. Append ("</soap:Body>"); } soap. Append ("</soap:Envelope>"); returnsoap. ToString (); } #endregion
Soap Simple Example