动态调用webservice(部分转载)

动态调用webservice,做个笔记:

 1 public class WSHelper
 2     {
 3         /// < summary>           
 4         /// 动态调用web服务         
 5         /// < /summary>          
 6         /// < param name="url">WSDL服务地址< /param> 
 7         /// < param name="methodname">方法名< /param>           
 8         /// < param name="args">参数< /param>           
 9         /// < returns>< /returns>          
10         public static object InvokeWebService(string url, string methodname, object[] args)
11         {
12             return WSHelper.InvokeWebService(url, null, methodname, args);
13         }
14         /// < summary>          
15         /// 动态调用web服务 
16         /// < /summary>          
17         /// < param name="url">WSDL服务地址< /param>
18         /// < param name="classname">类名< /param>  
19         /// < param name="methodname">方法名< /param>  
20         /// < param name="args">参数< /param> 
21         /// < returns>< /returns>
22         public static object InvokeWebService(string url, string classname, string methodname, object[] args)
23         {
24             string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
25             if ((classname == null) || (classname == ""))
26             {
27                 classname = WSHelper.GetWsClassName(url);
28             }
29             try
30             {                   //获取WSDL   
31                 WebClient wc = new WebClient();
32                 Stream stream = wc.OpenRead(url + "?WSDL");
33                 ServiceDescription sd = ServiceDescription.Read(stream);
34                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
35                 sdi.AddServiceDescription(sd, "", "");
36                 CodeNamespace cn = new CodeNamespace(@namespace);
37                 //生成客户端代理类代码          
38                 CodeCompileUnit ccu = new CodeCompileUnit();
39                 ccu.Namespaces.Add(cn);
40                 sdi.Import(cn, ccu);
41                 CSharpCodeProvider icc = new CSharpCodeProvider();
42                 //设定编译参数                 
43                 CompilerParameters cplist = new CompilerParameters();
44                 cplist.GenerateExecutable = false;
45                 cplist.GenerateInMemory = true;
46                 cplist.ReferencedAssemblies.Add("System.dll");
47                 cplist.ReferencedAssemblies.Add("System.XML.dll");
48                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
49                 cplist.ReferencedAssemblies.Add("System.Data.dll");
50                 //编译代理类                 
51                 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
52                 if (true == cr.Errors.HasErrors)
53                 {
54                     System.Text.StringBuilder sb = new System.Text.StringBuilder();
55                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
56                     {
57                         sb.Append(ce.ToString());
58                         sb.Append(System.Environment.NewLine);
59                     }
60                     throw new Exception(sb.ToString());
61                 }
62                 //生成代理实例,并调用方法   
63                 System.Reflection.Assembly assembly = cr.CompiledAssembly;
64                 Type t = assembly.GetType(@namespace + "." + classname, true, true);
65                 object obj = Activator.CreateInstance(t);
66                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
67                 return mi.Invoke(obj, args);
68                 // PropertyInfo propertyInfo = type.GetProperty(propertyname);     
69                 //return propertyInfo.GetValue(obj, null); 
70             }
71             catch (Exception ex)
72             {
73                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
74             }
75         }
76         private static string GetWsClassName(string wsUrl)
77         {
78             string[] parts = wsUrl.Split('/');
79             string[] pps = parts[parts.Length - 1].Split('.');
80             return pps[0];
81         }
82     }

需要在web.config中添加webservice的地址。

<add key="ConnetionHisExist" value="http://10.10.99.8:8808/HisIsExist.asmx"/>
<!--<add key="ConnetionUrlMsg" value="http://10.10.99.8:8082/MessageApi.asmx"/>-->
<add key="ConnetionUrlMsg" value="http://112.74.203.57:8000/MessageApi.asmx"/>

动态调用方式:

1、取得webconfig中的webservice的值

string MsgWebServiceurl = ConfigurationManager.AppSettings["ConnetionUrlMsg"].ToString();//取得webconfig中webservice值

2、组建字符串数组,访问

 1 string strxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
 2 "<data>" +
 3 "<OrderNo>" + dt.Rows[0]["order_no"].ToString() + "</OrderNo>" +
 4 "<MerBillNo>" + dt.Rows[0]["merchant_billno"].ToString() + "</MerBillNo>" +
 5 "<RefundMethod>" + "02" + "</RefundMethod>" +
 6 "<Amount>" + dt.Rows[0]["real_cost"].ToString() + "</Amount>" +
 7 "<OperUser>" + dt.Rows[0]["oper_user"].ToString() + "</OperUser>" +
 8 "<TermNo>" + dt.Rows[0]["termno"].ToString() + "</TermNo>" +
 9 "<HisRefundNo>" + dt.Rows[0]["merchant_billno"].ToString() + "</HisRefundNo>" +
10 "<HisNo>" + dt.Rows[0]["merchant_billno"].ToString() + "</HisNo>" +
11 "<ChannelTransNo>" + dt.Rows[0]["tran_no"].ToString() + "</ChannelTransNo>" +
12 "<PayAppSource>" + dt.Rows[0]["payappcode"].ToString() + "</PayAppSource>" +
13 "<PaySourceCode>" + dt.Rows[0]["paysourcecode"].ToString() + "</PaySourceCode>" +
14 "<Flushes>" + "1" + "</Flushes>" +//0-退款 1-冲正
15 "<SignType>" + "01" + "</SignType>" +
16 "<SignData>" + SignData + "</SignData>" +
17 "</data>";
18 
19 
20 
21 string[] argsMsg = new string[3];//组建字符串数组
22 argsMsg[0] = "F000105";//访问类型
23 argsMsg[1] = strxml;//入参
24 argsMsg[2] = "xml";//返回类型 xml ,json
25 object Msgresult = WSHelper.InvokeWebService(MsgWebServiceurl, "MuPayInterface", argsMsg);

3、接下来对返回的object转化为字符串,然后解析这个字符串xml。

4、调用其他入参不是xml类型的

1 string isExistWebServiceurl = ConfigurationManager.AppSettings["ConnetionHisExist"].ToString();//取得webconfig中webservice值
2 
3 string[] args = new string[1];
4 args[0] = tran_no;
5 
6 object result = WSHelper.InvokeWebService(isExistWebServiceurl, "HisExistsOrNot", args);

返回的result是一个int类型,进行转换后,即可用了。

 

posted @ 2016-11-29 09:46  雨花宝宝  阅读(229)  评论(0编辑  收藏  举报