加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

动态调用webservice的函数

发布时间:2020-12-17 02:40:42 所属栏目:安全 来源:网络整理
导读:/// summary /// call webmethod dynamically /// /summary /// param name="url"the url of the webservice/param /// param name="myNamespace"the namespace which the class belongs to /param /// param name="className"the class which the webmothed
        /// <summary>

        /// call webmethod dynamically

        /// </summary>

        /// <param name="url">the url of the webservice</param>

        /// <param name="myNamespace">the namespace which the class belongs to </param>

        /// <param name="className">the class which the webmothed belongs to</param>

        /// <param name="methodName">the method which you are calling</param>

        /// <param name="args">the parameters which the method takes</param>

        /// <returns>the object which the method you called returns</returns>

        protected object GetService(string url,string myNamespace,string className,string methodName,object[] args)

        {

            //1. 使用 WebClient 下载 WSDL 信息。

            WebClient web = new WebClient();

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





            //2. 创建和格式化 WSDL 文档。

            ServiceDescription description = ServiceDescription.Read(stream);

            //3. 创建客户端代理代理类。

            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();

            importer.ProtocolName = "Soap";

            importer.Style = ServiceDescriptionImportStyle.Client;

            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

            importer.AddServiceDescription(description,null,null); // 添加 WSDL 文档。

            //4. 使用 CodeDom 编译客户端代理类。

            CodeNamespace nmspace = new CodeNamespace(myNamespace);

            CodeCompileUnit unit = new CodeCompileUnit();

            unit.Namespaces.Add(nmspace);

            ServiceDescriptionImportWarnings warning = importer.Import(nmspace,unit);

            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            CompilerParameters parameter = new CompilerParameters();

            parameter.GenerateExecutable = false;

            parameter.GenerateInMemory = true;

            parameter.ReferencedAssemblies.Add("System.dll");

            parameter.ReferencedAssemblies.Add("System.XML.dll");

            parameter.ReferencedAssemblies.Add("System.Web.Services.dll");

            parameter.ReferencedAssemblies.Add("System.Data.dll");

            CompilerResults result = provider.CompileAssemblyFromDom(parameter,unit);

            //5使用 Reflection 调用 WebService。

            if (!result.Errors.HasErrors)

            {

                Assembly asm = result.CompiledAssembly;

                Type t = asm.GetType(myNamespace + "." + className);

                object o = Activator.CreateInstance(t);

                MethodInfo method = t.GetMethod(methodName);

                object tempObj = (method.Invoke(o,args));

                return tempObj;

            }

            else

            {

                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                foreach (System.CodeDom.Compiler.CompilerError ce in result.Errors)

                {

                    sb.Append(ce.ToString());

                    sb.Append(System.Environment.NewLine);

                }

                throw new Exception(sb.ToString());

            }

        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读