.Net动态调用WebService
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.CodeDom; using System.CodeDom.Compiler; using System.Net; using System.Web.Services; using System.Web.Services.Description; using Microsoft.CSharp; /// <summary> /// WebServiceHelper 的摘要说明 /// </summary> public class WebServiceHelper { public WebServiceHelper() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 动态调用WebService /// </summary> /// <param name="url">WebService地址</param> /// <param name="methodname">方法名(模块名)</param> /// <param name="args">参数列表</param> /// <returns>object</returns> public static object InvokeWebService(string url, string methodname, object[] args) { return InvokeWebService(url, null, methodname, args); } /// <summary> /// 动态调用WebService /// </summary> /// <param name="url">WebService地址</param> /// <param name="classname">类名</param> /// <param name="methodname">方法名(模块名)</param> /// <param name="args">参数列表</param> /// <returns>object</returns> public static object InvokeWebService(string url, string classname, string methodname, object[] args) { string @namespace = "ServiceBase.WebService.DynamicWebLoad"; if (classname == null || classname == "") { classname = WebServiceHelper.GetClassName(url); } ///动态调用类所执行的过程 //1.获取服务描述语言(WSDL) WebClient wc = new WebClient(); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream);//设置Web服务描述语言 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();//生成客户端代理类 sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace);//声明命名空间 //2.生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit();//为CodeDOM程序图形提供容器 ccu.Namespaces.Add(cn);//获取命名空间集合 sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider();//提供对 C# 代码生成器和代码编译器的实例的访问 ICodeCompiler icc = csc.CreateCompiler();//定义用于调用源代码编译的接口或使用指定编译器的 CodeDOM 树 //3.设定编译器的参数 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"); //4.编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);//使用指定的编译器设置编译程序集 if (true == cr.Errors.HasErrors) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //5.生成代理实例,并调用方法 System.Reflection.Assembly assembly = cr.CompiledAssembly;//获取或设置已编译的程序集 Type t = assembly.GetType(@namespace + "." + classname, true, true); object obj = Activator.CreateInstance(t);//为 COM 对象提供对方法的版本无关的访问 System.Reflection.MethodInfo mi = t.GetMethod(methodname);//通过反射获得方法名 return mi.Invoke(obj, args);//使用制定的参数调用当前实例所表示的方法,执行方法 } private static string GetClassName(string url) { string[] parts = url.Split('/'); string[] pps = parts[parts.Length - 1].Split('.'); return pps[0]; } } ------------------------------------------------------------------------------------------- protected void Button2_Click(object sender, EventArgs e) { string WebServiceUrl = "http://222.90.232.40:8899/webservice/WebService.asmx"; string WebServiceMethodname = "QueryData"; object[] args = new object[2]; args[0] = "select ID,JKZJMC from JKZJLB"; args[1] = "data source=oradb;uid=movepolicepdasc;pwd=movepolicepdasc"; DataSet result = (DataSet)WebServiceHelper.InvokeWebService(WebServiceUrl, WebServiceMethodname, args); GridView1.DataSource = result.Tables[0]; GridView1.DataBind(); } -------------------------------------------------------------------------------------------- [WebMethod] public void SendToUrl() { HttpContext.Current.Response.Redirect("http://222.90.232.40:8899/webservice/WebService.asmx", false); } |