京东签名认证Sign

  public static string SignToMd5(Dictionary @params, string secret)
        {
            var sortDic = new SortedDictionary(@params);
            var query = new StringBuilder(secret);
            foreach (var item in sortDic)
            {
                if (item.Key != "secretkey")
                {
                    if (string.IsNullOrEmpty(item.Key)) continue;
                    query.Append(item.Key).Append(item.Value);
                }
            }
            query.Append(secret);
            var md5 = MD5.Create();
            var bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
            return string.Join("", bytes.Select(x => x.ToString("X2")));
        }

当access_token为空时不用传入,要不然 会出现签名错误 

传参

 var signParams = new Dictionary
            {
                {"method", "jd.union.open.goods.jingfen.query"},
                {"app_key", appkey},
                {"timestamp", times},
                {"format", "json"},
                {"v", "1.0"},
                {"sign_method", "md5"}
            };
            var urlParams = string.Empty;
            signParams.Add("param_json", @"{""goodsReq"":{""eliteId"":""1""}}"); // param json 参与加密但是不参与url
            foreach (var item in signParams)
            {
                urlParams += $"&{item.Key}={HttpUtility.UrlEncode(item.Value.ToString(), Encoding.UTF8)}";
            }
            var sign = PddInterface.SignToMd5(signParams, secretkey);
            urlParams += "&sign=" + sign;

            var url = "https://router.jd.com/api?" + urlParams.TrimStart('&');
            jstring = PddInterface.HttpGetConnectToServer(url);

 

你可能感兴趣的:(c#,c#)