100分求解:c#访问https的webservice 在线急等!!

风灵灬 2016-05-04 10:03:19
使用wsdl生成代理类时就报错了,应该怎么弄,第一次接触https的webservice,服务应该是用java开发的



webservice地址:https://ross.ote.cnnic.cn:8443/cnnicAPI/services/registrant?wsdl

在线急等!!
...全文
5170 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿尔萨斯 2018-03-23
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Services.Description;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace Winning.Tjgl.ExternalInterface
{
    public static class WebServiceHelper
    {
        #region InvokeWebService
        //动态调用web服务
        public static object InvokeWebService(string url, string methodname, object[] args)
        {
            return WebServiceHelper.InvokeWebService(url, null, methodname, args);
        }

        public static object InvokeWebService(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();
                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 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);
                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);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
            }
        }

        /// <summary>   
        /// 调用WebService 包含SoapHeader
        /// </summary>   
        /// <param name="wsUrl">WebService地址</param>
        /// <param name="classname">类名</param>
        /// <param name="methodName">方法名称</param>   
        /// <param name="soapHeader">SOAP头</param>
        /// <param name="args">参数列表</param>   
        /// <returns>返回调用结果</returns>   
        public static object InvokeWebService(string wsUrl, string className, string methodName, SoapHeader soapHeader, object[] args)
        {
            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((className == null) || (className == ""))
            {
                className = GetWsClassName(wsUrl);
            }
            try
            {
                //获取WSDL   
                WebClient wc = new WebClient();
                Stream stream = wc.OpenRead(wsUrl + "?wsdl");
                System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.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 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);
                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);

                FieldInfo[] arry = t.GetFields();

                FieldInfo client = null;
                object clientkey = null;
                if (soapHeader != null)
                {
                    //Soap头开始   
                    client = t.GetField(soapHeader.ClassName + "Value");

                    //获取客户端验证对象   
                    Type typeClient = assembly.GetType(@namespace + "." + soapHeader.ClassName);

                    //为验证对象赋值   
                    clientkey = Activator.CreateInstance(typeClient);

                    foreach (KeyValuePair<string, object> property in soapHeader.Properties)
                    {
                        typeClient.GetField(property.Key).SetValue(clientkey, property.Value);
                    }
                    //Soap头结束   
                }

                //实例类型对象   
                object obj = Activator.CreateInstance(t);

                if (soapHeader != null)
                {
                    //设置Soap头
                    client.SetValue(obj, clientkey);
                }

                System.Reflection.MethodInfo mi = t.GetMethod(methodName);

                return mi.Invoke(obj, args);
            }
            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];
        }

        #endregion

        #region SOAP头

        /// <summary>
        /// SOAP头
        /// </summary>
        public class SoapHeader
        {
            /// <summary>
            /// 构造一个SOAP头
            /// </summary>
            public SoapHeader()
            {
                this.Properties = new Dictionary<string, object>();
            }

            /// <summary>
            /// 构造一个SOAP头
            /// </summary>
            /// <param name="className">SOAP头的类名</param>
            public SoapHeader(string className)
            {
                this.ClassName = className;
                this.Properties = new Dictionary<string, object>();
            }

            /// <summary>
            /// 构造一个SOAP头
            /// </summary>
            /// <param name="className">SOAP头的类名</param>
            /// <param name="properties">SOAP头的类属性名及属性值</param>
            public SoapHeader(string className, Dictionary<string, object> properties)
            {
                this.ClassName = className;
                this.Properties = properties;
            }

            /// <summary>
            /// SOAP头的类名
            /// </summary>
            public string ClassName { get; set; }

            /// <summary>
            /// SOAP头的类属性名及属性值
            /// </summary>
            public Dictionary<string, object> Properties { get; set; }

            /// <summary>
            /// 为SOAP头增加一个属性及值
            /// </summary>
            /// <param name="name">SOAP头的类属性名</param>
            /// <param name="value">SOAP头的类属性值</param>
            public void AddProperty(string name, object value)
            {
                if (this.Properties == null)
                {
                    this.Properties = new Dictionary<string, object>();
                }
                Properties.Add(name, value);
            }
        }
        #endregion

        //调用方法
        /*
        WebServiceHelper.SoapHeader header = new WebServiceHelper.SoapHeader("Variable");
        header.AddProperty("UserName", "aa");
        header.AddProperty("Password", "bb");
        object r = WebServiceHelper.InvokeWebService(
            "http://localhost:2996/WebService1.asmx",
            "HelloWorld",
            header,
            null);*/
    }
}
volcanobj 2018-01-25
  • 打赏
  • 举报
回复
我也遇到了,怎么解决得呢
volcanobj 2018-01-25
  • 打赏
  • 举报
回复
我也遇到了,怎么解决得呢
volcanobj 2018-01-25
  • 打赏
  • 举报
回复
我也遇到了,怎么解决得呢
zgcdxs 2017-07-28
  • 打赏
  • 举报
回复
我也一样的问题,请问楼上是怎么解决的。
bright_2099 2017-05-10
  • 打赏
  • 举报
回复
我也遇见了同样的问题,最后搞定了吗?
风灵灬 2016-05-05
  • 打赏
  • 举报
回复
有人知道吗??求解
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
引用 9 楼 lovelj2012 的回复:
wsdl.exe /l:cs /out:D:/Service1.cs https://ross.ote.cnnic.cn:8443/cnnicAPI/services/registrant?wsdl 在visual studio命令里面,执行上面的东东,会生成一个代理类到D盘
生不成啊
江南小鱼 2016-05-04
  • 打赏
  • 举报
回复
wsdl.exe /l:cs /out:D:/Service1.cs https://ross.ote.cnnic.cn:8443/cnnicAPI/services/registrant?wsdl 在visual studio命令里面,执行上面的东东,会生成一个代理类到D盘
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
http://blog.csdn.net/l_serein/article/details/8106677 然后添加引用后就可以用这个了
我试下
  • 打赏
  • 举报
回复

那就不要用wsdl的方式,通过vs添加web引用的方式添加呢
songbing774933 2016-05-04
  • 打赏
  • 举报
回复
引用 5 楼 zxk3200 的回复:
[quote=引用 3 楼 songbing774933 的回复:]
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);


private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}

这样不用生成代理类吗,不生成代理类怎么请求呢[/quote]

没用过wsdl....
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
引用 3 楼 songbing774933 的回复:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
这样不用生成代理类吗,不生成代理类怎么请求呢
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
引用 1 楼 songbing774933 的回复:
这个是远程证书问题啊 如果不想验证证书,就添加ServerCertificateValidationCallback,并且在回调函数内直接返回true
wsdl哪个参数可以不验证证书呢
songbing774933 2016-05-04
  • 打赏
  • 举报
回复
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);


private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
  • 打赏
  • 举报
回复
http://blog.csdn.net/l_serein/article/details/8106677 然后添加引用后就可以用这个了
songbing774933 2016-05-04
  • 打赏
  • 举报
回复
这个是远程证书问题啊

如果不想验证证书,就添加ServerCertificateValidationCallback,并且在回调函数内直接返回true
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
Authenticate pre-emptively 这个加密方式是什么意思,在c#里应该怎么写啊
江南小鱼 2016-05-04
  • 打赏
  • 举报
回复
额,我试了下,java的webservice确实不能这么玩 帮你度娘了个demo,自己瞧瞧吧
风灵灬 2016-05-04
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
http://blog.csdn.net/l_serein/article/details/8106677
然后添加引用后就可以用这个了

他这里需要用户名密码,类似这种


我用
cn.cnnic.domain.VspRegistrantDomainServiceClient vrd = new VspRegistrantDomainServiceClient();
vrd.ClientCredentials.Windows.ClientCredential.UserName = "zwanVSP";
vrd.ClientCredentials.Windows.ClientCredential.Password = "****";
vrd.ClientCredentials.SupportInteractive = true;

var a=vrd.vspDomainUpload("pp.com");


设置了用户名感觉无效呢

12,165

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧