【发布时间】:2017-10-24 07:00:10
【问题描述】:
我正在尝试为更大的项目编写一个简单的代码,该代码依赖于将数据发送到 php 网站,但结果始终为空,问题是两者都返回空结果,我将响应打印到标签但它返回空数组,
Xamarin 代码是
private async void sendphp()
{
Uri uri = new Uri("http://example.com/iservices/send.php");
var postData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("param1", "value"),
new KeyValuePair<string, string>("param2", "value")
};
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, uri);
req.Content = new FormUrlEncodedContent(postData);
HttpClient client = new HttpClient();
var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
lblInfo.Text = content;
}
PHP 代码是:
<?php
print_r($_POST);
print_r($_FILES);
?>
问题是两者都返回空结果,我将响应打印到标签但它返回空数组, 我检查了服务器响应它没问题,但帖子是空的,为什么?????? 我花了 4 天时间尝试了许多解决方案,但都失败了
【问题讨论】:
-
我知道 $_FILES 应该返回空数组,我在失去理智的时候就放了它,为什么 $_POST 返回空数组
-
我终于知道了问题所在,我在字符串中输入 https:// 而不是 http:// ,它可以工作
-
现在我可以使用 PostAsync Aslo 来发送 json 格式,就像这样的 php $json = file_get_contents('php://input'); $obj = json_decode($json); print_r($obj);感谢michael-sivolobov his answer in thread,现在我可以使用带有 HttpRequest 的 SendAsync 读取正常的 KeyvaluePair 我会将这个留给面临同样问题的每个人,我从很多地方收集答案,很抱歉长文本
标签: xamarin xamarin.forms xamarin-studio