通过RestSharp传递json urlencode

时间:2017-02-21 04:07:10

标签: c# asp.net json

我有这个代码,我试图通过RestSharp来传递API。

 const string task = "pay";
 const string command_api_token = "9ufks6FjffGplu9HbaN7uq6XXPPVQXBP";
 const string merchant_email_on_voguepay = "mymail@mail.com";

 Random rnd = new Random();
 string refl =  DateTime.Now + rnd.Next(0,9999999).ToString();
 byte[] hash_target = Encoding.Default.GetBytes(command_api_token + task + merchant_email_on_voguepay + refl);

 string hashD = BitConverter.ToString(new SHA512CryptoServiceProvider().ComputeHash(hash_target)).Replace("-", string.Empty).ToUpper();

 var keyValues = new Dictionary<string, string>
                    {
                         { "task", "pay"},
                         { "merchant", "3333-4444"},
                         { "ref",refl},
                         { "hash",hashD},
                         { "amount", "20"},
                         { "seller", "seller@mail.com"},
                         { "remarks", "payment"},                            

                    };

//serialization using Newtonsoft JSON 
  string json = JsonConvert.SerializeObject(keyValues);

//url encode the json
  var postString = Server.UrlEncode(json);

//calling API with Restsharp
  var client = new RestClient("https://voguepay.com/api/");
  var request = new RestRequest(Method.POST);
  request.AddParameter("json",json);
  IRestResponse response = client.Execute(request);

  Textbox1.Text = response.Content;

我认为我的代码安排并不是很好,因为我在每次移动时都会收到错误消息。

如果我尝试将其发布在上面,我会

  

“response”:“X006”,“description”:“无效的哈希”......

如果尝试“使用Restsharp调用API”中的“url编码json”,我会收到错误消息

  

“回复”:“X001”,“说明”:“商家ID无效”......

我认为我没有把事情做好,有人可以看看我的工作,并指出这段代码会出现什么问题?

1 个答案:

答案 0 :(得分:0)

I am using below code for calling API may this one help u.Here i am passing one class object u replace this by Dictionary and try..
public void insertData(OCMDataClass kycinfo, string clientId, string type)
    {
        try
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["CBService"]);
                string postBody = Newtonsoft.Json.JsonConvert.SerializeObject(kycinfo);
                var jsonString = JsonConvert.SerializeObject(kycinfo);
                var content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json");
                var myContent = JsonConvert.SerializeObject(kycinfo);
                var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
                var byteContent = new ByteArrayContent(buffer);
                var result = client.PostAsync("Bfilvid/api/SvcVId/CreateKYCRepository", content).Result;
                if (result.IsSuccessStatusCode)
                {
                    string resultContent = result.Content.ReadAsStringAsync().Result;
                }
                else
                {
                    string resultContent = result.Content.ReadAsStringAsync().Result;
                }
            }

        }
        catch (Exception ex)
        {

        }