C# 调用java Webservice「建议收藏」

C# 调用java Webservice「建议收藏」usingMicrosoft.CSharp;usingSystem;usingSystem.CodeDom;usingSystem.CodeDom.Compiler;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;us

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Services.Description;

namespace Common.WebService
{
    public class WSHelper
    {
        static string @namespace = "employee.webService.aykj.net";
        //static string @namespace = "net.aykj.webService.employee";

        /// < 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)
        {

            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;//run  
                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];
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181183.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月16日 下午11:00
下一篇 2022年10月16日 下午11:16


相关推荐

  • 图像传感器的 DVP 信号

    图像传感器的 DVP 信号一、DVP简述DVP是数字视频端口(digitalvideoport)的简称,传统的sensor输出接口,采用并行输出方式,DVP总线PCLK极限约在96M左右,所有DVP最大速率最好控制在72M以下,DVP是并口,需要PCLK、VSYNC、HSYNC、D[0:11]——可以是8/10/12bit数据,具体情况要看ISP或baseband是否支持。DVP接口在信号完整性方面受限制,速率也受限制。如图1所示,并口传输数据需要帧同步信号(Vsync

    2022年5月27日
    34
  • 计算机基础教程(一)

    计算机基础教程(一)大学计算机基础教程 PDF 下载链接链接 https pan baidu com s 1PGsvh1oRD1e 密码 zt4x 推荐先看下面的两本 计算机是怎样跑起来的程序是怎样跑起来的这两本百度云连接 CSDN 审核不通过 可以自己网上找 pdf 的

    2026年3月26日
    2
  • windows下ftp密码设置问题

    windows下ftp密码设置问题网上多数文章在介绍 FTP 安装时 都将认证设置为匿名 后面若想增加用户名 密码认证则需要注意 如果当前你的电脑设置了开机密码 那么 FTP 账户的密码必须和你开机密码一致 否则提示密码无效

    2026年3月16日
    3
  • Python读取h5文件_html python

    Python读取h5文件_html python原文链接:https://blog.csdn.net/leibaojiangjun1/article/details/53635353 h5接受的数据是矩阵跟mat方法一致,但是具有更强的压缩性能使用hdf5依赖于python的工具包:h5pyimporth5py #导入工具包importnumpyasnp#HDF5的写入:imgData=np.zeros((30,3…

    2025年10月11日
    5
  • JAVA枚举类型(Enum)的使用[通俗易懂]

    JAVA枚举类型(Enum)的使用[通俗易懂]在现实社会中,有些类的实例对象是固定的。例如季节,只有春夏秋冬。如果你创建了一个season类,你当然有义务维护这个类的实例对象只能是春(SPRING)、夏(SUMMER)、秋(AUTUMN)、冬(WINTER)这四个。这个时候就体现出枚举类的作用了,java中枚举类型就是针对这样的场景需求所设计的。/***枚举类的后缀建议为Enum,枚举类型的实例对象建议全大写(这样做符合JAVA的…

    2022年10月9日
    4
  • css 渐变下划线

    css 渐变下划线前端开发记录渐变下划线 border bottom 1pxsolid border image webkit linear gradient toright ffffff rgba 213 213 213 0 6 ffffff 1101 控制边框颜色渐变 border image moz linear gradient toright ffffff rgba 213 213 213 0 6 ffffff 1101 border i

    2026年3月17日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号