Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PostAsJsonAsync POST变量未到达Flight PHP REST服务器_C#_Json_Rest_Dotnet Httpclient_Flightphp - Fatal编程技术网

C# PostAsJsonAsync POST变量未到达Flight PHP REST服务器

C# PostAsJsonAsync POST变量未到达Flight PHP REST服务器,c#,json,rest,dotnet-httpclient,flightphp,C#,Json,Rest,Dotnet Httpclient,Flightphp,我已经安装了一个Flight PHP REST服务器。 在1端点处,它需要POST数据,并基于其中一个POST数据从数据库中检索一些数据,并将其作为JSON返回 当我在Chrome中使用Postman REST扩展时,我看到了正确的结果。但是当我使用我的C应用程序进行调用时,返回的json为null,因为$\u POST看起来是空的 这是my Flight index.php: Flight::route('POST /getText', function(){ // Create a

我已经安装了一个Flight PHP REST服务器。 在1端点处,它需要POST数据,并基于其中一个POST数据从数据库中检索一些数据,并将其作为JSON返回

当我在Chrome中使用Postman REST扩展时,我看到了正确的结果。但是当我使用我的C应用程序进行调用时,返回的json为null,因为$\u POST看起来是空的

这是my Flight index.php:

Flight::route('POST /getText', function(){  
  // Create a new report class:
  $reportText = new ReportText;
  $theText = $reportText->ParsePost($_POST);
  if ($theText == null)
  {
    echo null;
  }
  else
  {
    echo json_encode($theText);
  }        
});
这是我的帖子:

public function ParsePost($PostDictionary)
{
    $textArray = null;
    foreach ($PostDictionary as $key => $value)
    {
        if (!empty($value))
        {
            list($tmp, $id) = explode("_", $key);
            $text = $this->geFooWithText($id);
            $textArray[$key] = "bar";
        }
    }

    return $textArray;
}
这是我的C部分:

private static async Task RunAsyncPost(string requestUri)
{
    using (var client = new HttpClient())
    {
        // Send HTTP requests
        client.BaseAddress = new Uri("myUrl");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            // HTTP POST
            var response = await client.PostAsJsonAsync(requestUri, new { question_8 = "foo", question_9 = "bar" });
            response.EnsureSuccessStatusCode(); // Throw if not a success code.
            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(json))
                {
                    throw new ArgumentNullException("json", @"Response from the server is null");    
                }

                var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

                foreach (var kvp in dictionary)
                {
                    Debug.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
                }
            }
        }
        catch (HttpRequestException e)
        {
            // Handle exception.
            Debug.WriteLine(e.ToString());
            throw;
        }
    }
}
似乎我在使用C的通话中遗漏了一些东西

[更新]

在这篇文章中,同样的问题似乎也出现了。 有人建议使用小提琴

使用邮递员和x-www-form-urlencoded:

POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 29
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4

question_8=foo&question_9=bar
使用邮递员和表单数据:

POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 244
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvb4wmaP4KooT6TFu
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4

------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_8"

foo
------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_9"

bar
------WebKitFormBoundaryvb4wmaP4KooT6TFu--
使用我的C应用程序:

POST http://myHost/api/getText/ HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: myHost
Content-Length: 50
Expect: 100-continue
Connection: Keep-Alive

{"question_8":"foo","question_9":"bar"}

C应用程序正在以不同的方式发送它。

我发现了问题所在

因为我使用client.PostAsJsonAsync,所以POST数据以json的形式发送,正如您在Fiddler中看到的那样

PHP不希望POST数据采用json格式。 要读取这些数据,我需要使用$data=file\u get\u contents'php://input'; 在我的PHP文件中

现在我有了密钥和值,可以继续了。看起来PHP需要一个$\u JSON变量

POST http://myHost/api/getText/ HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: myHost
Content-Length: 50
Expect: 100-continue
Connection: Keep-Alive

{"question_8":"foo","question_9":"bar"}