从 Xamarin 获取 JSON 对象到 PHP?

Get JSON Object from Xamarin to PHP?

我想使用 HTTP Web POST 从我的表单向我的服务器插入数据。我的代码如下 我无法获取 JObject json 的值并将其发送到我的 php 代码中。

var caf = entCafNo.Text;
string url = "http://192.168.120.9:7777/TBS/Host=" + Constants.hostname + "&Database=" + Constants.database + "&Request=SendCaf";
string contentType = "application/json";
JObject json = new JObject
{
  { "CAF", caf }
};

HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

PHP代码:

$request = $_GET["Request"];

if($request == "SendCaf"){
    $caf = $_POST["CAF"];

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf)";
    mysqli_query ($conn, $sql);
}

您将所有数据都放入 URL。我认为这是 GET,而不是 POST.

我没有用过GetRequestStream,所以我没有答案。如果您对 POST 的另一种方式感兴趣,请使用 PostAsync。我在我当前的 Xamarin 项目中的做法是这样的

using Newtonsoft.Json;
using System.Net.Http;
//more code here

string url = "http://localhost/helloword/";
string contentType = "application/json";
JObject json = new JObject
{
    { "key1", value1},
    { "key2", value2 }            
};
HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));
// this is data received from server. May or may not need this.
var data = await response.Content.ReadAsStringAsync();

参考: