Right-click your site-〉 Add Service Reference-〉 and enter the address of WebService.
After that, you can use the object to manipulate your webservice.
Method Three: Get and post using the HTTP protocol
This is the most flexible approach.
View Code
Using System;
Using System.Collections;
Using System.IO;
Using System.Net;
Using System.Text;
Using System.Xml;
Using System.Xml.Serialization;
Namespace Bingosoft.RIA.Common
{
<summary>
Classes that use Webrequest/webresponse for webservice calls
</summary>
public class Webservicecaller
{
#region Tip: Instructions for use
WebServices should support get and post calls, and the following code should be added to the Web. config
<webServices>
<protocols>
<add name= "HttpGet"/>
<add name= "HttpPost"/>
</protocols>
</webServices>
Invocation Example:
Hashtable ht = new Hashtable (); Hashtable the set of parameters required for WebService
Ht. ADD ("str", "Test");
Ht. ADD ("B", "true");
XmlDocument xx = Websvccaller.querysoapwebservice ("Http://localhost:81/service.asmx", "HelloWorld", HT);
MessageBox.Show (xx. OuterXml);
#endregion
<summary>
Requires WebService support for post calls
</summary>
public static XmlDocument Querypostwebservice (String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URL + "/" + MethodName);
Request. Method = "POST";
Request. ContentType = "application/x-www-form-urlencoded";
Setwebrequest (Request);
byte[] data = Encodepars (Pars);
Writerequestdata (request, data);
return Readxmlresponse (Request. GetResponse ());
}
<summary>
Requires webservice support for get calls
</summary>
public static XmlDocument Querygetwebservice (String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URL + "/" + MethodName + "?" + parstostring (Pars));
Request. Method = "GET";
Request. ContentType = "application/x-www-form-urlencoded";
Setwebrequest (Request);
return Readxmlresponse (Request. GetResponse ());
}
<summary>
Universal WebService Call (SOAP), parameter pars is a string type parameter name, parameter value
</summary>
public static XmlDocument Querysoapwebservice (String URL, String MethodName, Hashtable Pars)
{
if (_xmlnamespaces.containskey (URL))
{
Return Querysoapwebservice (URL, MethodName, Pars, _xmlnamespaces[url]. ToString ());
}
Else
{
Return Querysoapwebservice (URL, MethodName, Pars, GetNamespace (URL));
}
}
private static XmlDocument Querysoapwebservice (String URL, String MethodName, Hashtable Pars, String XmlNs)
{
_xmlnamespaces[url] = xmlns;//Add cache for increased efficiency
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URL);
Request. Method = "POST";
Request. ContentType = "Text/xml; Charset=utf-8 ";
Request. Headers.add ("SOAPAction", "\" "+ XmlNs + (Xmlns.endswith ("/")? "": "/") + MethodName + "\" ");
Setwebrequest (Request);
byte[] data = Encodeparstosoap (Pars, XmlNs, MethodName);
Writerequestdata (request, data);
XmlDocument doc = new XmlDocument (), doc2 = new XmlDocument ();
doc = Readxmlresponse (request. GetResponse ());
XmlNamespaceManager mgr = new XmlNamespaceManager (Doc. NameTable);
Mgr. AddNamespace ("Soap", "http://schemas.xmlsoap.org/soap/envelope/");
String retxml = doc. selectSingleNode ("//soap:body/*/*", Mgr). INNERXML;
Doc2. LOADXML ("<root>" + retxml + "</root>");
Adddelaration (DOC2);
return doc2;
}
private static string GetNamespace (String URL)
{
HttpWebRequest request = (HttpWebRequest) webrequest.create (URL + "?) WSDL ");
Setwebrequest (Request);
WebResponse response = Request. GetResponse ();
StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding.UTF8);
XmlDocument doc = new XmlDocument ();
Doc. LoadXml (Sr. ReadToEnd ());
Sr. Close ();
return Doc. selectSingleNode ("//@targetNamespace"). Value;
}
private static byte[] Encodeparstosoap (Hashtable Pars, String XmlNs, String MethodName)
{
XmlDocument doc = new XmlDocument ();
Doc. LOADXML ("<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:Envelope>");
Adddelaration (DOC);
XmlElement soapbody = doc.createelement_x_x ("Soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement soapbody = doc. createelement ("Soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement Soapmethod = doc.createelement_x_x (MethodName);
XmlElement Soapmethod = doc. CreateElement (MethodName);
Soapmethod.setattribute ("xmlns", xmlns);
foreach (string k in Pars.keys)
{
XmlElement Soappar = doc.createelement_x_x (k);
XmlElement Soappar = doc. CreateElement (k);
Soappar.innerxml = Objecttosoapxml (Pars[k]);
Soapmethod.appendchild (Soappar);
}
Soapbody.appendchild (Soapmethod);
Doc. Documentelement.appendchild (Soapbody);
Return Encoding.UTF8.GetBytes (Doc. OuterXml);
}
private static string Objecttosoapxml (object o)
{
XmlSerializer myserializer = new XmlSerializer (O.gettype ());
MemoryStream ms = new MemoryStream ();
Myserializer.serialize (MS, O);
XmlDocument doc = new XmlDocument ();
Doc. LOADXML (Encoding.UTF8.GetString (Ms. ToArray ()));
if (Doc. DocumentElement = null)
{
return Doc. Documentelement.innerxml;
}
Else
{
return o.tostring ();
}
}
<summary>
Set voucher and timeout time
</summary>
<param name= "Request" ></param>
private static void Setwebrequest (HttpWebRequest request)
{
Request. Credentials = CredentialCache.DefaultCredentials;
Request. Timeout = 10000;
}
private static void Writerequestdata (HttpWebRequest request, byte[] data)
{
Request. ContentLength = data. Length;
Stream writer = Request. GetRequestStream ();
Writer. Write (data, 0, data. Length);
Writer. Close ();
}
private static byte[] Encodepars (Hashtable Pars)
{
Return Encoding.UTF8.GetBytes (parstostring (Pars));
}
private static String parstostring (Hashtable Pars)
{
StringBuilder sb = new StringBuilder ();
foreach (string k in Pars.keys)
{
if (sb.) Length > 0)
{
Sb. Append ("&");
}
Sb. Append (Httputility.urlencode (k) + "=" + Httputility.urlencode (Pars[k]. ToString ()));
}
Return SB. ToString ();
}
private static XmlDocument Readxmlresponse (WebResponse response)
{
StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding.UTF8);
String retxml = Sr. ReadToEnd ();
Sr. Close ();
XmlDocument doc = new XmlDocument ();
Doc. LOADXML (Retxml);
return doc;
}
private static void Adddelaration (XmlDocument doc)
{
XmlDeclaration decl = doc. Createxmldeclaration ("1.0", "Utf-8", null);
Doc. InsertBefore (Decl, Doc. DocumentElement);
}
private static Hashtable _xmlnamespaces = new Hashtable ()//cache XmlNamespace, avoid repeated calls GetNamespace
}
}
Call Webservice,post, Get method implementation in. Net