This method can makeProgramThe WebServices method is called without web reference.CodeTo dynamically call WebServices. Before using the SDK, reference the system. Web. Services dynamic link library, which is a. Net built-in DLL.
The method is as follows:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. net;
Using System. IO;
Using System. Web. Services. description;
Using System. codedom;
Using Microsoft. CSHARP;
Using System. codedom. compiler;
Namespace Testskin
{
Class WebServices
{
/// <Summary>
/// Instantiate WebServices
/// </Summary>
/// <Param name = "url"> WebServices address </Param>
/// <Param name = "methodname"> Call Method </Param>
/// <Param name = "ARGs"> Put the parameters required in WebServices in this object [] in order. </Param>
Public Static Object Invokewebservice ( String URL, String Methodname, Object [] ARGs)
{
// The namespace here is the namespace of the WebServices to be referenced. It is written to death. You can add a parameter to pass it in from the outside.
String @ Namespace = " Client " ;
Try
{
// Get WSDL
WebClient WC = New WebClient ();
Stream stream = WC. openread (URL + " ? WSDL " );
Servicedescription SD = Servicedescription. Read (Stream );
String Classname = SD. Services [ 0 ]. Name;
Servicedescriptionimporter SDI = New Servicedescriptionimporter ();
SDI. addservicedescription (SD, "" , "" );
Codenamespace CN = New Codenamespace (@ namespace );
// Generate client proxy code
Codecompileunit CCU = New Codecompileunit ();
CCU. namespaces. Add (CN );
SDI. Import (CN, CCU );
Csharpcodeprovider CSC = New Csharpcodeprovider ();
Icodecompiler ICC = CSC. createcompiler ();
// Set compilation Parameters
Compilerparameters cplist = New 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 " );
// Compile proxy class
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 ());
}
// Generate proxy instance and call Method
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);
}< br> catch
{< br> return null ;
}< BR >}