2017-02-21 108 views
0

我有這段代碼,我試圖用它來通過RestSharp傳遞一個API。通過RestSharp傳遞json urlencode

const string task = "pay"; 
const string command_api_token = "9ufks6FjffGplu9HbaN7uq6XXPPVQXBP"; 
const string merchant_email_on_voguepay = "[email protected]"; 

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", "[email protected]"}, 
         { "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; 

我認爲我的代碼的安排並不真的好,因爲我在每次移動時都會收到錯誤消息。

如果我嘗試發佈它,因爲它的正上方,我得到

「迴應」: 「X006」, 「說明」: 「無效的哈希」 ......

如果嘗試得到「URL編碼的JSON」參與的「與Restsharp調用API」 中,我得到錯誤消息

「迴應」: 「X001」,「d escription「:」無效的商戶ID「...

我想我沒有擺正事情,有人可以看看我的工作,並指出可能是這個代碼的問題是什麼?

回答

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) 
     { 

     }