将integer传递给FormUrlEncodedContent

本文关键字:FormUrlEncodedContent integer | 更新日期: 2023-09-27 18:01:21

我使用asp.net MVC和c#,在我的银行部分项目中,我使用这个代码进行银行

var requestContent = new FormUrlEncodedContent(new[] {
    new KeyValuePair<string, string>("amount", payment.Amount), //i want change this section for <string,int>
    new KeyValuePair<string, string>("transid", "id"),
    new KeyValuePair<string, string>("pin","pin" )
});
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync("http://address.com/api/verify/", requestContent);
HttpContent responseContent = response.Content;
string result = "";
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
    result = await reader.ReadToEndAsync();
}

现在我的银行场景是变化的,我应该传递整数作为金额,我怎么能从我的代码?因为FormUrlEncodedContent接受<string,string>谢谢您的帮助

将integer传递给FormUrlEncodedContent

试试这个,它对我有效。

        public class PaymentActionsRequest
        {
            [JsonProperty("payment_intent")]
            public string PaymentIntent { get; set; }
            
            [JsonProperty("amount")]
            public long Amount { get; set; }        
        }
      var keyValueContent = PaymentActionsRequest.ToKeyValue();
      var formUrlEncodedContent = new FormUrlEncodedContent(keyValueContent);

您需要使用类似Amount.ToString()的东西,因为该内容最终将被字符串/编码为POST主体的一部分。

相关文章:
  • 没有找到相关文章