示例#1
0
        /// <summary>
        /// Post
        /// </summary>
        /// <param name="dataModel"></param>
        /// <returns></returns>
        public static string CreatePost(IRequestModel dataModel)
        {
            if (dataModel == null)
            {
                throw new ArgumentNullException(nameof(dataModel));
            }

            var originData =
                $"Action={dataModel.Action}&Nonce={new Random(DateTime.Now.Millisecond).Next(10000, 99999)}&SecretId={AppConsts.SecretId}&SignatureMethod={AppConsts.SignatureMethod.ToString()}&Timestamp={DateTimeOffset.Now.ToUnixTimeSeconds()}";
            var data = $"{AppConsts.Gateway}?{originData}";

            var serializeData = JsonConvert.SerializeObject(dataModel, new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
            var json = JObject.Parse(serializeData);
            var dic  = new SortedDictionary <string, string>();

            foreach (var item in json)
            {
                dic.Add(item.Key, item.Value.Value <string>());
            }

            dic.Remove("action");
            StringBuilder sb = new StringBuilder();

            foreach (var dicKey in dic.Keys)
            {
                sb.Append($"{dicKey}={dic[dicKey]}&");
            }
            serializeData = sb.ToString().TrimEnd(new[] { '&' });

            if (!string.IsNullOrEmpty(serializeData))
            {
                data       = $"{data}&{serializeData}";
                originData = $"{originData}&{serializeData}";
            }
            var encryptData = $"POST{data}";
            var signature   = AppConsts.SignatureMethod == HmacType.HmacSHA1
                ? HmacUtil.EncryptWithSHA1(encryptData, AppConsts.SecretKey)
                : HmacUtil.EncryptWithSHA256(encryptData, AppConsts.SecretKey);

            var resultData = $"{originData}&Signature={WebUtility.UrlEncode(signature)}";

            return(resultData);
        }