runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

【移动支付】S1工具类

 

HashUtil:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// 1111
    /// </summary>
    public static class HashUtil
    {

        public static string GetMd5(string src)
        {
            //1
            MD5 md =   MD5.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(src);
            byte[] buffer2 = md.ComputeHash(bytes);
            string str = "";
            for (int i = 0; i < buffer2.Length; i++)
            {
                str = str + buffer2[i].ToString("x2");
            }
            return str;

        }

        public static IDictionary<string, string> ModelToDic<T1>(T1 cfgItem)
        {
            IDictionary<string, string> sdCfgItem = new Dictionary<string, string>();

            System.Reflection.PropertyInfo[] cfgItemProperties = cfgItem.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
            {
                string name = item.Name;
                object value = item.GetValue(cfgItem, null);
                if (value != null && (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    sdCfgItem.Add(name, value.ToString());
                }
            }

            return sdCfgItem;
        }

        public static IDictionary<string, string> AsciiDictionary(IDictionary<string, string> sArray)
        {
            //1
            IDictionary<string, string> asciiDic = new Dictionary<string, string>();
            string[] arrKeys = sArray.Keys.ToArray();
            Array.Sort(arrKeys, string.CompareOrdinal);
            foreach (var key in arrKeys)
            {
                string value = sArray[key];
                asciiDic.Add(key, value);
            }
            return asciiDic;
        }

        public static string BuildQueryString(IDictionary<string, string> sArray)
        {
            //1
            //拼接 K=V&A=B&c=1 这种URL

            StringBuilder sc = new StringBuilder();

            foreach (var item in sArray)
            {
                string name = item.Key;
                string value = item.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    sc.AppendFormat("{0}={1}&", name, value);
                }

            }

            string fnlStr = sc.ToString();
            fnlStr = fnlStr.TrimEnd('&');

            return fnlStr;
        }


        public static string HmacSHA256_base64(String message, String secret)
        {
            string aa = "";

            byte[] myKey = Encoding.UTF8.GetBytes(secret);

            byte[] myData = Encoding.UTF8.GetBytes(message);


            HMACSHA256 sha256_HMAC = new HMACSHA256(myKey);
            byte[] hashed = sha256_HMAC.ComputeHash(myData);
            //byte[] 根据实际情况转成字符串
            //对E进行Base64编码获得F,F即为签名内容,算法公式为F=Base64_Encode(E)。
            aa = Convert.ToBase64String(hashed);

            return aa;
        }

        /// <summary>
        /// 打印键值对
        /// </summary>
        /// <param name="sArray"></param>
        /// <returns></returns>
        public static string PrintKV(IDictionary<string, string> sArray)
        {
            //A=1
            //B=2
            //C=3
            //这种形式
            StringBuilder sc = new StringBuilder();
            foreach (var item in sArray)
            {
                string name = item.Key;
                string value = item.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    sc.AppendLine(string.Format("{0}={1}", name, value));
                }
            }
            string fnlStr = sc.ToString();
            return fnlStr;
        }

    }
}

 

 

HttpUtil

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CommonUtils
{
    public static class HttpUtil
    {
        public static string HttpPostJson(string url, string body, IDictionary<string, string> header, int timeOut, IDictionary<string, string> rspHeader)
        {
            string rst = "";
            Encoding enc1 = Encoding.UTF8;
            byte[] content = enc1.GetBytes(body);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            request.Timeout = timeOut * 1000;
            request.ReadWriteTimeout = timeOut * 1000;
            request.KeepAlive = false;
            request.ServicePoint.Expect100Continue = false;
            request.ContentLength = content.Length;
            //设置请求header
            foreach (var item in header)
            {
                request.Headers.Add(item.Key, item.Value);
            }

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(content, 0, content.Length);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, enc1);
                rst = reader.ReadToEnd();
            }

            //收集响应header
            if (rspHeader == null)
            {
                rspHeader = new Dictionary<string, string>();
            }
            foreach (var item in response.Headers.AllKeys)
            {
                rspHeader.Add(item, response.Headers[item]);
            }

            if (response != null)
            {
                response.Close();
                response = null;
            }
            if (request != null)
            {
                request.Abort();//2018-7-31,增加
                request = null;
            }

            return rst;
        }

        public static string HttpPostJson(string url, string body, int timeOut)
        {
            string rst = "";
            Encoding enc1 = Encoding.UTF8;
            byte[] content = enc1.GetBytes(body);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            request.Timeout = timeOut * 1000;
            request.ReadWriteTimeout = timeOut * 1000;
            request.KeepAlive = false;
            request.ServicePoint.Expect100Continue = false;
            request.ContentLength = content.Length;
            //设置请求header
            //foreach (var item in header)
            //{
            //    request.Headers.Add(item.Key, item.Value);
            //}

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(content, 0, content.Length);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, enc1);
                rst = reader.ReadToEnd();
            }

            //收集响应header
            //if (rspHeader == null)
            //{
            //    rspHeader = new Dictionary<string, string>();
            //}
            //foreach (var item in response.Headers.AllKeys)
            //{
            //    rspHeader.Add(item, response.Headers[item]);
            //}

            if (response != null)
            {
                response.Close();
                response = null;
            }
            if (request != null)
            {
                request.Abort();//2018-7-31,增加
                request = null;
            }

            return rst;
        }


        public static string HttpPostXml(string url, string content, int timeOut)
        {

            HttpWebRequest req = null;
            HttpWebResponse res = null;


            byte[] bytes = Encoding.UTF8.GetBytes(content);

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Timeout = timeOut * 1000;
            req.ReadWriteTimeout = timeOut * 1000;
            req.Method = "POST";
            req.KeepAlive = false;
            req.ServicePoint.Expect100Continue = false;
            req.ContentLength = bytes.Length;
            using (Stream putStream = req.GetRequestStream())
            {
                putStream.Write(bytes, 0, bytes.Length);
            }

            res = req.GetResponse() as HttpWebResponse;
            string strreturn = string.Empty;
            using (Stream stream = res.GetResponseStream())
            {
                StreamReader sr = new StreamReader(stream);
                strreturn = sr.ReadToEnd();
            }

            if (res != null) res.Close();
            if (req != null) req.Abort();

            return strreturn;
        }


    }
}

 

。。

。。

posted on 2025-03-09 19:24  runliuv  阅读(13)  评论(0)    收藏  举报