[英]What is the equivalent of this REST PHP code in C#?
我正在使用 REST API,它們的示例代碼是 PHP 格式。 我對 PHP 一無所知。 我已經寫了一個 HttpClient 代碼,但響應不是我所期望的。 這是PHP代碼:
$data = array("merchant_id" => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"amount" => 1000,
"callback_url" => "http://www.yoursite.com/verify.php",
"description" => "خرید تست",
"metadata" => [ "email" => "info@email.com","mobile"=>"09121234567"],
);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
if (empty($result['errors'])) {
if ($result['data']['code'] == 100) {
header('Location: https://www.zarinpal.com/pg/StartPay/' . $result['data']["authority"]);
}
} else {
echo'Error Code: ' . $result['errors']['code'];
echo'message: ' . $result['errors']['message'];
}
}
?>
到目前為止,這是我的 C# 代碼:
string MerchantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string CallbackURL = "www.mysite.com/backurl";
string data = $"{\"merchant_id\": \"{MerchantId}\", \"amount\": \"1000\",
\"callback_url\": \"{CallbackURL}\"";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsondata = serializer.Serialize(data);
var stringContent = new StringContent(jsondata, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var response = await client.PostAsync("https://api.zarinpal.com/pg/v4/payment/request.json", stringContent);
var result = await response.Content.ReadAsStringAsync();
這就是我得到的result
:
"{"data":[],"errors":{"code":-9,"message":"輸入參數無效,驗證錯誤。","validations":[{"merchant_id":"商戶id字段為必填項。"},{"amount":"金額字段為必填項。"},{"callback_url":"回調url字段為必填項。"},{"description":"說明字段為必填項。 “}]}}”
我究竟做錯了什么?
我可以在您的 c# 代碼中看到一些可能的問題來源,但我不能肯定地說,因為您的代碼段不可編譯。
我猜你得到了錯誤的 json 序列化。
你應該在jsondata
賦值之后放置一個斷點,並確保這個變量的實際值正好是:
"{\"merchant_id\":\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\",\"amount\":1000,\"callback_url\":\"www.mysite.com/backurl\"}"
您正在使用 c# 內插字符串(前綴$
)來替換jsondata
變量的值。 但是,無論何時使用內插字符串,字符{
和}
都是有意義的,用於捕獲替換值。
當您執行$"{\\"merchant_id\\":
, {
之后的任何內容都必須是值或變量,並且您正在插入轉義字符\\"
,這是錯誤的。 在這種情況下可以使用的是內插逐字字符串(前綴$@
)。
但不是嘗試手動編寫 json,c# 的最佳實踐是使用System.Text.Json
進行序列化,使用方法JsonSerializer.Serialize
,您可以在其中傳遞有意義的結構化對象,如類、結構或字典.
在這個例子中,我使用了一個匿名對象:
string MerchantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string CallbackURL = "www.mysite.com/backurl";
var data = new { merchant_id = MerchantId, amount = 1000, callback_url = CallbackURL };
var jsondata = JsonSerializer.Serialize(data);
var stringContent = new StringContent(jsondata, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var response = await client.PostAsync("https://api.zarinpal.com/pg/v4/payment/request.json", stringContent);
var result = await response.Content.ReadAsStringAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.