C # There are many ways to call WebService:
1. Add a WebService reference directly to the project. This method is simple, but not dynamic. That is, you must add a reference again after each service address or content change.
2. To use SoapHttpClientProtocol, you must Reference the Reference generated by adding a WebService. cs-class service interfaces are integrated into the Custom Service call class. The service call class inherits from SoapHttpClientProtocol. if the service interface changes, you need to modify the service call class. The sample code is as follows:
public class MySoapHttpClientProtocol : SoapHttpClientProtocol { public MySoapHttpClientProtocol(string url) { Url = url; } [SoapHeader("ClientContext")] [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = MyNamespace, ResponseNamespace = MyNamespace, Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] [return: System.Xml.Serialization.XmlElementAttribute("out", IsNullable = true)] public string myMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string in0) { try { object[] results = this.Invoke("getConnection", new object[] { in0 }); return ((string)(results[0])); } catch (Exception ex) { } } }
Here, myMethod is copied from Reference. cs referenced by the Service. Of course, you can also write it by yourself, which is easier to copy.
3. Dynamic call. The sample code is as follows:
////// WebService proxy class ///Public class WebServiceAgent {private object agent; private Type agentType; private const string CODE_NAMESPACE = "Beyondbit. WebServiceAgent. Dynamic ";///
/// Call the specified method //////
Method Name, case sensitive///
Parameters. Values are assigned in the order of parameters.///
Web Service Return Value
Public object Invoke (string methodName, params object [] args) {MethodInfo mi = agentType. GetMethod (methodName); return this. Invoke (mi, args );}///
/// Call the specified method //////
Method Information///
Parameters. Values are assigned in the order of parameters.///
Web Service Return Value
Public object Invoke (MethodInfo method, params object [] args) {return method. invoke (agent, args);} public MethodInfo [] Methods {get {return agentType. getMethods ();}}}
You only need to specify the service URL, interface name, and parameters for dynamic calling.