zoukankan      html  css  js  c++  java
  • 带"签名"的请求接口实现

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

     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

  • 相关阅读:
    dumpsys
    阿里云云效流水线体验
    停车入场城市排行榜1
    第三方企业号对接工作
    PHP搭建(windows64+apache2.4.7+mysql-5.6+php5.5)
    十大编程算法助程序员走上高手之路
    数据库的最简单实现
    JavaScript 开发的45个经典技巧
    常用meta整理
    Shell脚本编程初体验
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/10965012.html
Copyright © 2011-2022 走看看