.NET MAUI Android连接API遇400错误,Xamarin版本正常运行
问题排查:Xamarin迁移.NET MAUI后Token请求返回400 Bad Request
关键差异对比
从你提供的请求/响应信息中,能看到两个核心差异点:
- HTTP协议版本:Xamarin请求使用HTTP/1.1,而.NET MAUI请求默认使用HTTP/2.0
- 请求内容长度:成功请求的
Content-Length为79,失败请求为78,说明两次发送的表单内容存在字节级差异
针对性排查方案
强制HttpClient使用HTTP/1.1
部分老版本IIS(如你使用的IIS 10.0)对HTTP/2兼容性可能存在问题,可在MAUI中通过配置HttpClientHandler指定协议版本:var handler = new HttpClientHandler(); handler.DefaultRequestVersion = HttpVersion.Version11; handler.MaxRequestVersion = HttpVersion.Version11; var client = new HttpClient(handler);核对请求表单的具体内容
虽同为FormUrlEncodedContent,但可能存在编码或参数细节差异:- 检查参数完整性(如用户名、密码、
grant_type等是否与Xamarin完全一致) - 排查特殊字符的URL编码差异(Xamarin与MAUI的编码规则可能略有不同)
- 直接输出表单字符串对比内容:
var content = new FormUrlEncodedContent(yourParameters); string contentString = await content.ReadAsStringAsync(); Debug.WriteLine(contentString);
- 检查参数完整性(如用户名、密码、
解析400响应的具体错误信息
失败响应的Content-Length为82,其中的JSON内容包含API返回的具体拒绝原因,务必解析查看:var response = await client.PostAsync(...); if (!response.IsSuccessStatusCode) { string errorContent = await response.Content.ReadAsStringAsync(); Debug.WriteLine(errorContent); }核对NuGet包版本
重点对比System.Net.Http、Microsoft.Extensions.Http等HTTP相关包的版本,确保MAUI项目的包版本与Xamarin项目兼容,或尝试回退到Xamarin使用的包版本测试。
附请求/响应原始信息
成功请求(Xamarin)
{Method: POST, RequestUri: 'http://api.xxx.net/token', Version: 1.1, Content: System.Net.Http.FormUrlEncodedContent, Headers: { Content-Type: application/x-www-form-urlencoded Content-Length: 79 }} {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type, Authorization Access-Control-Allow-Methods: * Access-Control-Allow-Origin: * Cache-Control: no-cache Date: Sun, 17 Mar 2024 18:37:42 GMT Pragma: no-cache Server: Microsoft-IIS/10.0 X-Android-Received-Millis: 1710700662659 X-Android-Response-Source: NETWORK 200 X-Android-Selected-Protocol: http/1.1 X-Android-Sent-Millis: 1710700662393 X-Powered-By: ASP.NET X-Powered-By-Plesk: PleskWin Content-Length: 541 Content-Type: application/json; charset=UTF-8 Expires: -1 }}
失败请求(.NET MAUI)
{Method: POST, RequestUri: 'http://api.xxx.net/token', Version: 2.0, Content: System.Net.Http.FormUrlEncodedContent, Headers: { Content-Type: application/x-www-form-urlencoded Content-Length: 78 }} {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type, Authorization Access-Control-Allow-Methods: * Access-Control-Allow-Origin: * Cache-Control: no-cache Date: Sun, 17 Mar 2024 18:27:50 GMT Pragma: no-cache Server: Microsoft-IIS/10.0 X-Android-Received-Millis: 1710700070765 X-Android-Response-Source: NETWORK 400 X-Android-Selected-Protocol: http/1.1 X-Android-Sent-Millis: 1710700069943 X-Powered-By: ASP.NET X-Powered-By-Plesk: PleskWin Content-Length: 82 Content-Type: application/json; charset=UTF-8 Expires: -1 }}
内容的提问来源于stack exchange,提问作者Joey007
相关产品推荐
相关产品推荐

