Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. xml;
Using System. net;
Using System. Web. Services. description;
Using System. codedom;
Using System. codedom. compiler;
Using System. reflection;
Namespace Windowsservicewebdefaulthotcity
{
/// <Summary>
/// WebService proxy
/// </Summary>
Public Class Webserviceagent
{
Private Object Agent;
Private Type agenttype;
Private Const String Code_namespace = " Beyondbit. webserviceagent. Dynamic " ;
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "url"> </param>
Public Webserviceagent ( String URL)
{
Xmltextreader Reader = New Xmltextreader (URL + " ? WSDL " );
//Create and format a wsdl document
Servicedescription SD=Servicedescription. Read (Reader );
//Create a client proxy
Servicedescriptionimporter SDI= NewServicedescriptionimporter ();
SDI. addservicedescription (SD,Null,Null);
// Use codedom to compile client proxy classes
Codenamespace CN = New Codenamespace (code_namespace );
Codecompileunit CCU = New Codecompileunit ();
CCU. namespaces. Add (CN );
SDI. Import (CN, CCU );
Microsoft. CSHARP. csharpcodeprovider ICC = New Microsoft. CSHARP. csharpcodeprovider ();
Compilerparameters CP = New Compilerparameters ();
Compilerresults cr = ICC. compileassemblyfromdom (CP, CCU );
Agenttype = Cr. compiledassembly. gettypes ()[ 0 ];
Agent = Activator. createinstance (agenttype );
}
/// <Summary>
/// Call the specified Method
/// </Summary>
/// <Param name = "methodname"> Method Name, case sensitive </Param>
/// <Param name = "ARGs"> Parameters. Values are assigned in the order of parameters. </Param>
/// <Returns> Web Service Return Value </Returns>
Public Object Invoke ( String Methodname, Params Object [] ARGs)
{
Methodinfo Mi = Agenttype. getmethod (methodname );
Return This . Invoke (MI, argS );
}
/// <Summary>
/// Call a specified Method
/// </Summary>
/// <Param name = "method"> Method Information </Param>
/// <Param name = "ARGs"> Parameters. Values are assigned in the order of parameters. </Param>
/// <Returns> Web Service Return Value </Returns>
Public Object Invoke (methodinfo method, Params Object [] ARGs)
{
Return Method. Invoke (agent, argS );
}
Public Methodinfo [] Methods
{
Get
{
Return Agenttype. getmethods ();
}
}
}
}
Code
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
UsingSystem. text;
UsingSystem. Windows. forms;
Namespace Windowsapplication1
{
Public Partial Class Form1: Form
{
Private String _ URL = " Http://www.baidu.com " ;
Public Form1 ()
{
Initializecomponent ();
Init_data ();
}
Public Void Init_data ()
{
Windowsservicewebdefaulthotcity. webserviceagent agent = New Windowsservicewebdefaulthotcity. webserviceagent (_ URL );
Object [] ARGs = New Object [ 6 ];
ARGs [ 0 ] = " Pek " ;
ARGs [ 1 ] = " Can " ;
ARGs [ 2 ] = "" ;
ARGs [ 3 ] = " 2008-08-02 " ;
ARGs [ 4 ] = " 00: 00 " ;
ARGs [ 5 ] = " Own_9588 " ;
String Text = Agent. Invoke ( " Getallflight " , ArgS). tostring ();
Textbox1.text = Text;
}
}
}