【问题标题】:How to correctly convert from PHP and CURL post to C# HTTP Client post for groupon API如何正确地将 PHP 和 CURL 帖子转换为用于 groupon API 的 C# HTTP 客户端帖子
【发布时间】:2017-10-17 10:20:45
【问题描述】:

我已尝试转换以下代码

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

      }
   }

使用 C# 中的 HTTP 客户端,使用如下形式的 url 编码内容类代替 PHP 中的 $datatopost 数组

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 代码中所示。

任何人都能够提供一些想法,说明为什么这不能按预期工作,因为我现在对此一无所知,在此之前尝试了多种不同的方法来做到这一点?

【问题讨论】:

  • 你忘记了ci_lineitem_id上的“s”

标签: c# php json curl groupon


【解决方案1】:

除非是拼写错误,否则您在 c# 代码中的第三个参数末尾忘记了一个 s。

ci_lineitem_ids 在你的 php 中

ci_lineitem_id 在 c# 中

【讨论】:

  • 简单的拼写错误会导致问题结束。在投票结束之前,我一直在等待确保情况确实如此。
  • 我已经接受了答案,因为这是代码中的错字。我将不得不等待实际测试此更改,但毫无疑问它会起作用。
猜你喜欢
  • 2021-10-05
  • 2018-09-17
  • 2016-06-16
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 2011-11-09
相关资源
最近更新 更多