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

  • 相关阅读:
    UVA 818 Cutting Chains 切断圆环链 (暴力dfs)
    UVA 211 The Domino Effect 多米诺效应 (回溯)
    UVA225 Golygons 黄金图形(dfs+回溯)
    UVA208 Firetruck 消防车(并查集,dfs)
    UVA11212 EditingaBook ( IDA*搜索)
    UVA 140 Brandwidth 带宽 (dfs回溯)
    uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
    UVA10410 TreeReconstruction 树重建 (dfs,bfs序的一些性质,以及用栈处理递归 )
    cdoj 414 八数码 (双向bfs+康拓展开,A*)
    UVA 246 10-20-30 10-20-30游戏 模拟+STL双端队列deque
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/10965012.html
Copyright © 2011-2022 走看看