当前位置: 首页 > 工具软件 > HTTP Sign > 使用案例 >

京东签名认证Sign

柴博
2023-12-01
  public static string SignToMd5(Dictionary<string, object> @params, string secret)
        {
            var sortDic = new SortedDictionary<string, object>(@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<string, object>
            {
                {"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);

 

 类似资料: