欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# .Net动态调用webService实现思路及代码

程序员文章站 2024-03-03 17:45:04
复制代码 代码如下: using system; using system.collections.generic; using system.linq; using sy...
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.net;
using system.io;
using system.web.services.description;
using system.codedom;
using microsoft.csharp;
using system.codedom.compiler;
using system.reflection;
namespace https
{
public class wshelper
{
/// < summary>
/// 动态调用web服务
/// < /summary>
/// < param name="url">wsdl服务地址< /param>
/// < param name="methodname">方法名< /param>
/// < param name="args">参数< /param>
/// < returns>< /returns>
public static object invokewebservice(string url, string methodname, object[] args)
{
return wshelper.invokewebservice(url, null, methodname, args);
}
/// < summary>
/// 动态调用web服务
/// < /summary>
/// < param name="url">wsdl服务地址< /param>
/// < param name="classname">类名< /param>
/// < param name="methodname">方法名< /param>
/// < param name="args">参数< /param>
/// < returns>< /returns>
public static object invokewebservice(string url, string classname, string methodname, object[] args)
{
string @namespace = "enterpriseserverbase.webservice.dynamicwebcalling";
if ((classname == null) || (classname == ""))
{
classname = wshelper.getwsclassname(url);
}
try
{ //获取wsdl
webclient wc = new webclient();
stream stream = wc.openread(url + "?wsdl");
servicedescription sd = servicedescription.read(stream);
servicedescriptionimporter sdi = new servicedescriptionimporter();
sdi.addservicedescription(sd, "", "");
codenamespace cn = new codenamespace(@namespace);
//生成客户端代理类代码
codecompileunit ccu = new codecompileunit();
ccu.namespaces.add(cn);
sdi.import(cn, ccu);
csharpcodeprovider icc = new csharpcodeprovider();
//设定编译参数
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");
//编译代理类
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 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);
// propertyinfo propertyinfo = type.getproperty(propertyname);
//return propertyinfo.getvalue(obj, null);
}
catch (exception ex)
{
throw new exception(ex.innerexception.message, new exception(ex.innerexception.stacktrace));
}
}
private static string getwsclassname(string wsurl)
{
string[] parts = wsurl.split('/');
string[] pps = parts[parts.length - 1].split('.');
return pps[0];
}
}
}

调用
复制代码 代码如下:

string url = "http://webservice.webxml.com.cn/webservices/traintimewebservice.asmx";
string[] args = new string[2];
args[0] = "k123";
args[1] = "";
object result = wshelper.invokewebservice(url, "getdetailinfobytraincode", args);
dataset ds = (dataset)result;
this.gridview1.datasource = ds;
this.gridview1.databind();