Xamarin Forms - 简单的 PostAsync() 返回错误 400 bad request

我已经检查过所有类似的问题(https://www.google.com.au/search?q=httpclient%20bad%20request%20stack%20overflow&oq=httpclient%20bad%20request%20stack%20overflow&aqs=chrome..69i57j0j69i60l3j0.6218j0j4&sourceid=chrome&ie=UTF-8),虽然它们很相似,但没有找到解决方案。

问题:

我从我的 Xamarin 移动应用程序中,使用 HttpClient() 向 PHP 服务器发送了一个非常简单的 POST 请求。目前,PHP 服务器只是打印了任何它收到的 POST 请求。

当我使用 Postman 向我的 Web 服务器发送 POST 请求时,PHP 工作得非常好。但是,如果我从我的移动应用程序中发送 POST 请求,将导致错误:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Fri, 03 Aug 2018 08:52:36 GMT Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d54d4c18d07eeae5179fde8e4533dd6881533286355; expires=Sat, 03-Aug-19 08:52:35 GMT; path=/; domain=.braz.io; HttpOnly; Secure Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare CF-RAY: 44478c8b9d0c1d68-MEL Content-Type: text/html }}

public class RestService
{
    HttpClient client;
    private const string URL = "https://braz.io/mobile.php";

    public RestService()
    {
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
    }

    public async Task<string> getUserInformation(string username, string password)
    {
        var uri = new Uri(string.Format(URL, string.Empty));
        try
        {
            StringContent s = new StringContent("HELLO WORLD");
            var response = await client.PostAsync(uri, s);
        }
        catch (Exception ex)
        {Console.WriteLine(ex);}
        return null;
    }
}

似乎我在使用 PostAsync(); 函数上缺少了细节。为什么我可以从 Postman 成功进行 POST 请求,但在使用 PostAsync() 在我的移动应用程序时失败了呢?

在 Stack Overflow 上的讨论后:

我添加了 string body = await response.Content.ReadAsStringAsync(); 来查看 web 服务器是否向我的移动应用程序发送了有关错误的有用信息。不幸的是,它只返回了一个简单的 ERROR 400 webpage

  <html>\r\n<head><title>400 Bad Request</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>400 Bad Request</h1></center>\r\n<hr><center>openresty/1.11.2.4</center>\r\n</body>\r\n</html>\r\n"
点赞
用户2017550
用户2017550

下面是我某种方式偶然发现的解决方案(如果有人能在评论中写出/编辑此答案为什么这是解决方案,那就太好了!)

右键单击 Android 项目 -> Android 选项 -> Advanced -> 将 HttpClient implementationDefault 更改为 Android

2018-08-03 11:40:44