• 带"签名"的请求接口实现


    废话少说,直接上代码(⊙﹏⊙)

     class Program
        {
            //签名证书
            public static X509Certificate2 cerSigneCert;
            private static char[] hexChars;
            public Program()
            {
                cerSigneCert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + @"resources20016100001118204.p12", "");
            }
            static void Main(string[] args)
            {
                string xml = "xml字符串 <SIGNED_MSG />";
                string newXML = SHA1withRSA(xml);
                string url = "访问地址";
                string requestUrl = RequestUrl(url, newXML);
    
            }
    
            /// <summary>
            /// 签名
            /// PS:(SIGNED_MSG是属性的名称)
            /// </summary>
            /// <param name="xml"></param>
            /// <returns></returns>
            private static string SHA1withRSA(string xml)
            {
                string oldXML = xml.Replace("<SIGNED_MSG />", "");
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                byte[] msg = sha1.ComputeHash(Encoding.GetEncoding("GBK").GetBytes(oldXML));
    
                RSAPKCS1SignatureFormatter signe = new RSAPKCS1SignatureFormatter();
    
                signe.SetKey(cerSigneCert.PrivateKey);//设置用于签名的私钥
                signe.SetHashAlgorithm("SHA1");
                var signeText = ToHex(signe.CreateSignature(msg));//创建签名
    
                string newXML = xml.Replace("<SIGNED_MSG />", "<SIGNED_MSG>" + signeText + "</SIGNED_MSG>");
                return newXML;
            }
    
            private static string ToHex(byte[] ba)
            {
                if (ba == null) return "";
                char[]
                    buf = new char[ba.Length * 2];
    
                int p = 0;
                foreach (byte b in ba)
                {
                    buf[p++] = hexChars[b >> 4];
                    buf[p++] = hexChars[b & 0x0f];
                }
                return new string(buf);
            }
    
    
            /// <summary>
            /// 发送请求,获取响应
            /// </summary>
            /// <param name="url"></param>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string RequestUrl(string url, string data)//发送方法
            {
                var request = WebRequest.Create(url) as HttpWebRequest;
    
                request.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                request.KeepAlive = false;
    
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
    
                request.Method = "post";
                request.ContentType = "text/xml";
                request.Headers.Add("charset:gbk");
                var encoding = Encoding.GetEncoding("GBK");
    
                if (data != null)
                {
                    byte[] buffer = encoding.GetBytes(data);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                }
                else
                {
    
                }
    
                using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
                    {
                        string strResult = reader.ReadToEnd();
                        return strResult;
                    }
                }
            }
    
            private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
            {
                System.Console.WriteLine("Warning, trust any certificate");
                //为了通过证书验证,总是返回true
                return true;
            }
    
        }

     有关XML转换,大家请参考上一篇文章: https://www.cnblogs.com/shuai7boy/p/10963734.html

  • 相关阅读:
    ASP.NET MVC中多种ActionResult用法总结
    jQuery中异步操作对象Deferred
    jQuery中bind方法和live方法区别解析
    深入理解Javascript中this, prototype, constructor
    SQL及常见的三种类型注释
    SQLServer的两个日期相减(间隔)datediff函数
    SQLServer查询进程与死锁语句
    SqlServer获取当前日期的详细写法
    SQL中 Decode 和 Sign 语法的简单用法
    数据仓库模型之CDM、LDM与PDM的区别
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/10965012.html
Copyright © 2020-2023  润新知