Xamarin表单-简单的PostAsync()返回错误400错误的请求 [英] Xamarin Forms - simple PostAsync() returns error 400 bad request
问题描述
我已经检出了 似乎我没有使用 关于SO的一些讨论之后: 我添加了 这是我偶然发现的解决方案(如果有人可以在评论中写/编辑此答案,为什么这是个很棒的解决方案!) 右键单击Android项目-> Android选项->高级->将 I have checked out every one of these similar questions and although they are similar, no solutions have been found. The Problem: I have implemented a very simple post request from my Xamarin mobile application to a PHP server, using When I use {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
}}
It seems like I am missing a nuance with the After some discussion on SO: I added
Here is the solution I somehow stumbled upon (if someone could write in the comments / edit this answer for why this is the solution that'd be great!) Right Click Android Project -> Android Options -> Advanced -> Change 这篇关于Xamarin表单-简单的PostAsync()返回错误400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
公共类RestService{HttpClient客户端;私有常量字符串URL ="https://braz.io/mobile.php";公共RestService(){客户端=新的HttpClient();client.MaxResponseContentBufferSize = 256000;}公共异步Task< string>getUserInformation(字符串用户名,字符串密码){var uri = new Uri(string.Format(URL,string.Empty));尝试{StringContent s = new StringContent("HELLO WORLD");var response =等待client.PostAsync(uri,s);}抓住(前例外){Console.WriteLine(ex);}返回null;}}
PostAsync();
函数的细微差别.为什么在移动应用程序上使用 PostAsync()
时,我可以成功执行来自 Postman
的Post请求,但失败了? string body = await response.Content.ReadAsStringAsync();
来查看Web服务器是否正在将有关该错误的任何有用信息发送回我的移动应用程序.不幸的是,它只是发回了一个简单的 ERROR 400网页
< html> \ r \ n< head>< title> 400错误请求</title></head> \ r \ n< body bgcolor = \"white \"> \ r\ n< center> h1> 400错误请求</h1>/center/openresty/1.11.2.4< center> \ r \ n</body>\ r \ n</html> \ r \ n
HttpClient实现
从 Default
更改为 Android
HttpClient()
. The PHP server simply prints out any post request it is given, for now. Postman
to send my web server post requests, the PHP works perfectly, however attempting to send post requests from my mobile application results in an error:
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();
function. Why can I successfully do Post requests from Postman
but fail when using PostAsync()
on my mobile application? string body = await response.Content.ReadAsStringAsync();
to see if the web server was sending any useful information about the error, back to my mobile application. Unfortunately it just sent back a simple 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"
HttpClient implementation
from Default
to Android