<summary>
Sends a SOAP request and returns the response XML
</summary>
<param name= "url" > Request address </param>
<param name= "DATASTR" >soap Request information </param>
<returns> Return response Information </returns>
public static string Getsoapresource (string url, string datastr)
{
Initiating a request
Uri uri = new uri (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);
}
Response
WebResponse WebResponse = Webrequest.getresponse ();
using (StreamReader Mystreamreader = new StreamReader (WebResponse.GetResponseStream (), Encoding.UTF8))
{
string result = "";
return Result=mystreamreader.readtoend ();
}
}
Example: Call WebService to query IP address information
WebService Address: http://www.wjg121.cn/Service/IPAddress.asmx?op=GetIPCountryAndLocal
static void Main (string[] args)
{
Constructing SOAP Request Information
StringBuilder soap = new StringBuilder ();
Soap. Append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
Soap. Append ("<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. Append ("<soap:Body>");
Soap. Append ("<getipcountryandlocal xmlns=\" http://tempuri.org/\ ">");
Soap. Append ("<RequestIP>183.39.119.90</RequestIP>");
Soap. Append ("</GetIPCountryAndLocal>");
Soap. Append ("</soap:Body>");
Soap. Append ("</soap:Envelope>");
String url = "Http://www.wjg121.cn/Service/IPAddress.asmx";
Console.WriteLine (Webserviceutility.getsoapresource (Url,soap. ToString ()));
Console.readkey ();
}
return Result:
<?xml version= "1.0" encoding= "Utf-8"? ><soap:envelope xmlns:soap= "Http://schemas.
xmlsoap.org/soap/envelope/"xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance
"Xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "><soap:body><getipcountryandlocalr
Esponse xmlns= "http://tempuri.org/" ><GetIPCountryAndLocalResult> Guangdong Telecom </geti
Pcountryandlocalresult></getipcountryandlocalresponse></soap:body></soap:envelop
E>
Go C # sends SOAP requests over HTTP