对接第三方API,无SDK时,通用签名范例

1、包装PostHttp请求

 public string PPost(string url, string param, Method method, HtmlEncoding sendEncoding, HtmlEncoding receivEncoding, string referer, ref CookieContainer cookie, string contentType = "application/x-www-form-urlencoded") 
{            
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader sr = null;
            Stream reqStream = null;
            string html = string.Empty;
            try {
                byte[] bs = Encoding.GetEncoding(sendEncoding.ToString() == "UTF8" ? "UTF-8" : sendEncoding.ToString()).GetBytes(param);
                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cookie;
                request.Method = method.ToString();
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                request.ContentLength = bs.Length;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
                request.ContentType = contentType;
                request.Referer = referer;
                request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
                if (bs.Length > 0) {
                    reqStream = request.GetRequestStream();
                    reqStream.Write(bs, 0, bs.Length);
                }
                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                sr = new StreamReader(stream, Encoding.GetEncoding(receivEncoding.ToString() == "UTF8" ? "UTF-8" : receivEncoding.ToString()));
                html = sr.ReadToEnd();
            } catch {
                html = "timeout";
            } finally {
                if (response != null)  response.Close();           
                if (stream != null)  stream.Dispose();
                if (sr != null)  sr.Dispose();
                if (reqStream != null)   reqStream.Dispose();
                if (request != null)    request.Abort();
            }
            return html;
 }

2、签名

private void MakeSign(Dictionary<string, string> _param) 
{
            string paramStr = string.Join("", _param.Where(w => !string.IsNullOrEmpty(w.Value)).OrderBy(o => o.Key).Select(s => s.Key + s.Value).ToList<string>());
            paramStr = string.Format("{0}{1}{0}", ServiceConfig.AppkeyDict[_param["client_id"]], paramStr);
            string sign = EncodingHelper.MD5Encrypt(paramStr);
            _param["sign"] = sign;
            return _param;
}

public static string MD5Encrypt(string str)
{
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString().ToUpper();
 }

3、暴露请求接口

public string XSRequest(Dictionary<string, string> _param) {
            _param=this.MakeSign(_param);
            string param = string.Join("&", _param.Where(w => !string.IsNullOrEmpty(w.Value)).Select(s => s.Key + "=" + s.Value).ToList<string>());
            return collect.CollectHtml(_server_url, param, HttpHelper.Method.POST, HttpHelper.HtmlEncoding.UTF8, HttpHelper.HtmlEncoding.UTF8);
        }

4、其中参数为字典,其中包含所有参数:公共参数和各个方法的私有参数都放里面

一般参数中都需要时间戳:

_param["timestamp"] = ConvertDateTimeInt(DateTime.Now).ToString();

public static long ConvertDateTimeInt(System.DateTime time) 
{
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000000;            
            return t;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑羽酣天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值