Remember to reference system. Web. Services
Public class transferwebservice
{
/// <Summary>
/// Call the remote WebService Method Based on the specified information
/// </Summary>
/// <Param name = "url"> HTTP address of WebService </param>
/// <Param name = "namespace"> namespace of the WebService to be called </param>
/// <Param name = "classname"> name of the WebService class to be called (excluding the namespace prefix) </param>
/// <Param name = "methodname"> Method Name of the WebService to be called </param>
/// <Param name = "ARGs"> parameter list </param>
/// <Returns> execution result of WebService </returns>
/// <Remarks>
/// If the call fails, an exception is thrown. During the call, an exception is intercepted as appropriate.
/// Exception information may occur in two places:
/// 1. compileassembly fails when a WebService is dynamically constructed.
/// 2. the WebService itself fails to be executed.
/// </Remarks>
/// <Example>
/// <Code>
/// Object OBJ = invokewebservice ("http: // localhost: 4554/service1.asmx", "webservice1", "service1", "helloworld", new object [] {1, 2 });
/// </Code>
Public static object invokewebservice (string URL, string @ namespace, string classname, string methodname, object [] ARGs)
{
Try
{
System. net. WebClient WC = new system. net. WebClient ();
System. Io. Stream stream = WC. openread (URL + "? WSDL ");
System. Web. Services. description. servicedescription SD = system. Web. Services. description. servicedescription. Read (Stream );
System. Web. Services. description. servicedescriptionimporter SDI = new system. Web. Services. description. servicedescriptionimporter ();
SDI. addservicedescription (SD ,"","");
System. codedom. codenamespace Cn = new system. codedom. codenamespace (@ namespace );
System. codedom. codecompileunit CCU = new system. codedom. codecompileunit ();
CCU. namespaces. Add (CN );
SDI. Import (CN, CCU );
Microsoft. CSHARP. csharpcodeprovider CSC = new Microsoft. CSHARP. csharpcodeprovider ();
System. codedom. compiler. icodecompiler ICC = CSC. createcompiler ();
System. codedom. compiler. compilerparameters cplist = new system. codedom. compiler. compilerparameters ();
Cplist. generateexecutable = false;
Cplist. generateinmemory = true;
Cplist. referencedassemblies. Add ("system. dll ");
Cplist. referencedassemblies. Add ("system. xml. dll ");
Cplist. referencedassemblies. Add ("system. Web. Services. dll ");
Cplist. referencedassemblies. Add ("system. Data. dll ");
System. codedom. compiler. compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU );
If (true = Cr. errors. haserrors)
{
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
Foreach (system. codedom. compiler. compilererror Ce IN Cr. Errors)
{
SB. append (Ce. tostring ());
SB. append (system. environment. newline );
}
Throw new exception (sb. tostring ());
}
System. reflection. Assembly = Cr. compiledassembly;
Type T = assembly. GetType (@ namespace + "." + classname, true, true );
Object OBJ = activator. createinstance (t );
System. reflection. methodinfo MI = T. getmethod (methodname );
Return mi. Invoke (OBJ, argS );
}
Catch (exception ex)
{
Throw new exception (ex. innerexception. Message, new exception (ex. innerexception. stacktrace ));
}
}
}