Dynamics Webservice Call with Credential

Dynamics Webservice call with credential

 

 

    /// <summary>

    ///WebServiceHelper 的摘要说明

    /// </summary>

    public class WebServiceHelper

    {

        #region InvokeWebService

 

        /// <summary>

        /// 动态调用web服务

        /// </summary>

        /// <param name="url">URL</param>

        /// <param name="methodname">方法名</param>

        /// <param name="args">参数</param>

        /// <returns>object</returns>

        public static object InvokeWebService(string user,string password, string domain, string url, string methodname, object[] args)

        {

            return WebServiceHelper.InvokeWebService(user, password, domain, url, null, methodname, args);

        }

 

        /// <summary>

        /// 动态调用web服务

        /// </summary>

        /// <param name="url">URL地址</param>

        /// <param name="classname">类名</param>

        /// <param name="methodname">方法名</param>

        /// <param name="args">方法参数</param>

        /// <returns>方法返回值</returns>

        public static object InvokeWebService(string user, string password, string domain, string url, string classname, string methodname, object[] args)

        {

            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";

 

            if ((classname == null) || (classname == ""))

            {

                classname = WebServiceHelper.GetWsClassName(url);

            }

 

            try

            {

                //获取WSDL

                WebClient wc = new WebClient();

 

 

                wc.Credentials = new NetworkCredential(user,password,domain);

               

 

                Stream stream = wc.OpenRead(url + "?WSDL");

                ServiceDescription sd = ServiceDescription.Read(stream);

                ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();

 

                sdi.ProtocolName = "Soap"; //指定访问协议。

                sdi.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。

                sdi.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

 

 

                sdi.AddServiceDescription(sd, "", "");

                CodeNamespace cn = new CodeNamespace(@namespace);

 

                //生成客户端代理类代码

                CodeCompileUnit ccu = new CodeCompileUnit();

                ccu.Namespaces.Add(cn);

                sdi.Import(cn, ccu);

                CSharpCodeProvider csc = new CSharpCodeProvider();

 

                ICodeCompiler icc = csc.CreateCompiler();

                //设定编译参数

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

                //CompilerResults cr = csc.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);

                ParameterInfo[] pars = mi.GetParameters();

                for (int i = 0; i < pars.Length; i++)

                {

                    if (args[i].GetType().Name != pars[i].ParameterType.Name)

                    {

                        args[i] = Convert.ChangeType((object)args[i], pars[i].ParameterType);

                    }

                }

 

               

 

                Type type = obj.GetType();

                PropertyInfo propInfo = obj.GetType().GetProperty("Credentials");

                propInfo.SetValue(obj, new NetworkCredential(user, password, domain), null);

 

 

                return mi.Invoke(obj, args);

            }

            catch (Exception ex)

            {

                return "Error-" + ex.Message + ex.InnerException.Message;

            }

 

 

        }

 

        /// <summary>

        ///   GetWsClassName 

        /// </summary>

        /// <param name="strUrl">URL</param>

        /// <returns>ClassName</returns>

        private static string GetWsClassName(string strUrl)

        {

            string[] parts = strUrl.Split('/');

            string[] pps = parts[parts.Length - 1].Split('.');

            return pps[0];

        }

        #endregion

    }

 

转载于:https://www.cnblogs.com/lingdanglfw/p/3400537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值