c# - 如何正确地将 PHP 和 CURL 帖子转换为 groupon API 的 C# HTTP 客户端帖子

标签 c# php json curl groupon

我尝试转换以下代码

<?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/

相关文章:

c# - 系统.Net.WebException : The underlying connection was closed: The connection was closed unexpectedly

c# - Android.Views.InflateException : Binary XML file line #1: Binary XML file line #1: Error inflating class android. support.v7.widget.DrawerLayout

带有 symfony2 项目的 phpstorm xdebug

php - 我想获取我刚刚插入的行的 ID。我可以知道怎么做吗

php - 代码运行了 5 次。它应该只运行一次。 (PHP、MySQL)

python - json读取错误json.decoder.JSONDecodeError : Invalid\escape

java - 在 Android 上使用超链接 API 的最佳方式是什么?

c# - 限定数组的转换步骤数

json - 如何使用 JSON 转换器在 Play 上展平 js 数组?

c# - 组合框按 ID 选择项目但显示名称