2017-02-21 20 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 「エスケープ」:無効な販売者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) 
     { 

     } 
関連する問題