当前位置: 移动技术网 > IT编程>开发语言>.net > C# .Net动态调用webService实现思路及代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

56aiav,《美国地理》竟称重庆最有性格,皇冠现金租售13217599948

复制代码 代码如下:

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();

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网