예제 #1
0
        public static bool CheckSign(SortedDictionary <string, object> sortedDictionary, string key)
        {
            //如果没有设置签名,则跳过检测
            object obj;

            sortedDictionary.TryGetValue("sign", out obj);
            if (obj == null)
            {
                WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData签名不存在!");
                throw new WxPayException("WxPayData签名不存在!");
            }
            //如果设置了签名但是签名为空,则抛异常
            else if (sortedDictionary["sign"] == null || sortedDictionary["sign"].ToString() == "")
            {
                WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData签名存在但不合法!");
                throw new WxPayException("WxPayData签名存在但不合法!");
            }

            //获取接收到的签名
            string return_sign = sortedDictionary["sign"].ToString();

            //在本地计算新的签名
            string cal_sign = MakeSign(sortedDictionary, key);

            if (cal_sign == return_sign)
            {
                return(true);
            }

            WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData签名验证错误!");
            throw new WxPayException("WxPayData签名验证错误!");
        }
예제 #2
0
        public static string MakeSign(SortedDictionary <string, object> sortedDictionary, string key)
        {
            string buff = "";

            foreach (KeyValuePair <string, object> pair in sortedDictionary)
            {
                if (pair.Value == null)
                {
                    WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData内部含有值为null的字段!");
                    throw new WxPayException("WxPayData内部含有值为null的字段!");
                }

                if (pair.Key != "sign" && pair.Value.ToString() != "")
                {
                    buff += pair.Key + "=" + pair.Value + "&";
                }
            }
            buff = buff.Trim('&');

            //在string后加入API KEY
            buff += "&key=" + key;
            //MD5加密
            var md5 = MD5.Create();
            var bs  = md5.ComputeHash(Encoding.UTF8.GetBytes(buff));
            var sb  = new StringBuilder();

            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2"));
            }
            //所有字符转为大写
            return(sb.ToString().ToUpper());
        }
예제 #3
0
        public static string ToXml(SortedDictionary <string, object> sortedDictionary)
        {
            //数据为空时不能转化为xml格式
            if (0 == sortedDictionary.Count)
            {
                WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData数据为空!");
                throw new WxPayException("WxPayData数据为空!");
            }

            string xml = "<xml>";

            foreach (KeyValuePair <string, object> pair in sortedDictionary)
            {
                //字段值不能为null,会影响后续流程
                if (pair.Value == null)
                {
                    WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData内部含有值为null的字段!");
                    throw new WxPayException("WxPayData内部含有值为null的字段!");
                }

                if (pair.Value.GetType() == typeof(int))
                {
                    xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
                }
                else if (pair.Value.GetType() == typeof(string))
                {
                    xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
                }
                else//除了string和int类型不能含有其他数据类型
                {
                    WxLogHelper.Error(typeof(WxPayHelper).ToString(), "WxPayData字段数据类型错误!");
                    throw new WxPayException("WxPayData字段数据类型错误!");
                }
            }
            xml += "</xml>";
            return(xml);
        }
예제 #4
0
        public static string Post(string url, string xml, int timeout)
        {
            System.GC.Collect(); //垃圾回收,回收没有正常关闭的http连接

            string result = "";  //返回结果

            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;
            Stream          reqStream = null;

            try
            {
                //设置最大连接数
                ServicePointManager.DefaultConnectionLimit = 200;
                //设置https验证方式
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                        new RemoteCertificateValidationCallback(CheckValidationResult);
                }

                /***************************************************************
                 * 下面设置HttpWebRequest的相关属性
                 * ************************************************************/
                request = (HttpWebRequest)WebRequest.Create(url);

                request.Method  = "POST";
                request.Timeout = timeout * 1000;

                //设置POST的数据类型和长度
                request.ContentType = "text/xml";
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                request.ContentLength = data.Length;

                //往服务器写入数据
                reqStream = request.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();

                //获取服务端返回
                response = (HttpWebResponse)request.GetResponse();

                //获取服务端返回数据
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd().Trim();
                sr.Close();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                WxLogHelper.Error("HttpService", "Thread - caught ThreadAbortException - resetting.");
                WxLogHelper.Error("Exception message: {0}", e.Message);
                System.Threading.Thread.ResetAbort();
            }
            catch (WebException e)
            {
                WxLogHelper.Error("HttpService", e.ToString());
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    WxLogHelper.Error("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode);
                    WxLogHelper.Error("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription);
                }
                throw new WxPayException(e.ToString());
            }
            catch (Exception e)
            {
                WxLogHelper.Error("HttpService", e.ToString());
                throw new WxPayException(e.ToString());
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return(result);
        }