为什么我的 PHP 网站没有收到我从 Android Xamarin 应用发送 POST 的变量?

Why doesn't my PHP website receive the variable that I send POST from an Android Xamarin app?

我看不到从 Visual Basic 2019 Xamarin Forms 应用程序发送到 PHP REST 的结果。基本上这就是我在 Mainpage.xaml.cs:

中所做的
protected override async void OnAppearing()
        {
            HttpClient client = new HttpClient();
            Uri uri = new Uri("https://www.miweb.com/prueba.php");
            Msj mensaje = new Msj { Mensaje = "PRUEBAAAAAA" };
            string json = JsonConvert.SerializeObject(mensaje);
            var content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            //HttpResponseMessage response = client.PostAsync(uri, content).Result;
            using (var response = await client.PostAsync(uri, content))
            {
            }

            base.OnAppearing();
        }

The class Msj:

    internal class Msj
    {
        public string Mensaje { get; internal set; }
    }

这是 PHP 代码:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $input = $_POST;
    header("HTTP/1.1 200 OK");
    $file = fopen("app.txt", "w");
    fwrite($file, json_encode($input['Mensaje']) . PHP_EOL);
    fclose($file);
    exit();
}

已创建 app.txt 文件,但其内容为 "null"(不带引号。)

要查看已发送的数据,您可以使用:

print_r($_POST);

据此您应该能够找出 is/isn 未发送的内容并从那里更改您的代码。

最后我这样解决了,没有JSON。感谢所有人:

protected override async void OnAppearing()
{
    HttpClient client = new HttpClient();
    Uri uri = new Uri("https://www.miweb.com/prueba.php");
    var datos = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("mensaje", "SOLVED!")
    };
    var content = new FormUrlEncodedContent(datos);
    using (var response = await client.PostAsync(uri, content))
    {
    }
    base.OnAppearing();
}