我无法看到我从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);
从这里你应该可以找出哪些数据没有被发送,然后修改你的代码。
最后,我解决了这样的问题,没有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();
}