Post 请求 php Xamarin.Forms
Post request to php Xamarin.Forms
我在静态 class 中有这个函数,它向我服务器上的 php 代码发送 post 请求:
public static string phone;
public static async Task<string> CheckPhone()
{
string url = "my url...";
var form = new CheckPhoneForm { Phone = phone };
var content = JsonConvert.SerializeObject(form);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(url, new StringContent(content));
if (response.IsSuccessStatusCode == false) return "ERROR";
return await response.Content.ReadAsStringAsync();
}
CheckPhoneForm class:
public class CheckPhoneForm
{
public string Phone { get; set; }
}
php代码:
<?php
$phone = $_POST["Phone"];
//rest of the code.....
?>
出于某种原因,php 文件中的 $phone 没有接收到从 c# 代码发送的数据,它保持为空。
谁能告诉我我做错了什么?
在填充 $_POST 时,您可能没有以 PHP“识别”的两种格式之一发送数据。
您需要发送 application/x-www-form-urlencoded
或 multipart/form-data
的请求。
(发送其他 Content-Types
的请求正文仍然可以在 PHP 中读取,但这需要通过 php://input
发生,并且必须对内容进行任何解析手动完成。例如,发送 JSON 数据时,这是一种常用的方法。)
我在静态 class 中有这个函数,它向我服务器上的 php 代码发送 post 请求:
public static string phone;
public static async Task<string> CheckPhone()
{
string url = "my url...";
var form = new CheckPhoneForm { Phone = phone };
var content = JsonConvert.SerializeObject(form);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(url, new StringContent(content));
if (response.IsSuccessStatusCode == false) return "ERROR";
return await response.Content.ReadAsStringAsync();
}
CheckPhoneForm class:
public class CheckPhoneForm
{
public string Phone { get; set; }
}
php代码:
<?php
$phone = $_POST["Phone"];
//rest of the code.....
?>
出于某种原因,php 文件中的 $phone 没有接收到从 c# 代码发送的数据,它保持为空。 谁能告诉我我做错了什么?
在填充 $_POST 时,您可能没有以 PHP“识别”的两种格式之一发送数据。
您需要发送 application/x-www-form-urlencoded
或 multipart/form-data
的请求。
(发送其他 Content-Types
的请求正文仍然可以在 PHP 中读取,但这需要通过 php://input
发生,并且必须对内容进行任何解析手动完成。例如,发送 JSON 数据时,这是一种常用的方法。)