我尝试转换以下代码
<?php
// requires PHP cURL http://no.php.net/curl
$datatopost = array (
"supplier_id" => "1",
"token" => "xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT",
"ci_lineitem_ids" => json_encode ( array (54553919, 54553920) ),
);
$ch = curl_init ("https://scm.commerceinterface.com/api/v4/mark_exported");
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
if( $response ) {
$response_json = json_decode( $response );
if( $response_json->success == true ) {
//Successfully marked as exported (only items which are not already marked exported
} else {
}
}
通过使用如下形式的 url 编码内容类代替 PHP 中的 $datatopost
数组,在 C# 中使用 HTTP 客户端
FormUrlEncodedContent markExportedContent = new FormUrlEncodedContent(new[] {
new KeyValuePair<string,string>("supplier_id",country.supplier_id),
new KeyValuePair<string, string>("token",country.token),
new KeyValuePair<string, string>("ci_lineitem_id", JsonConvert.SerializeObject(country.ci_lineitem_ids))
});
然后使用 http 客户端将其发布到 API 但是我得到以下响应
{"reason": "Missing Parameters (ci_lineitem_ids).", "reason_code": 400, "success": false}
我认为它与 Newtonsoft Json 中的 JsonConvert.SerializeObject(country.ci_lineitem_ids)
有关我使用的包将字符串数组转换为 json 编码数组,如 PHP 代码中所示。
任何人都能够提供一些想法,说明为什么这没有按预期工作,因为我现在已经完全没有想法了,在此之前尝试了多种不同的方法来做到这一点?
最佳答案
除非是拼写错误,否则您在 C# 代码中忘记了第三个参数末尾的 s。
ci_lineitem_ids 在你的 php 中
ci_lineitem_id 在c#中
关于c# - 如何正确地将 PHP 和 CURL 帖子转换为 groupon API 的 C# HTTP 客户端帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46787849/