Xamarin 表单 ASP.NET android 模拟器无法访问本地主机 API
Xamarin forms ASP.NET android emulator cannot access localhost API
我正在开发一个使用 ASP.NET Web 应用程序模板的 Xamarin 表单应用程序。我 followed this tutorial 在 YouTube 上。我在使用 PostAsync 方法时遇到问题。 Web api 本身可以通过我本地计算机上的 IIS Chrome 正常启动,但不能通过模拟器启动。我打印了响应消息,显示为
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 26 Sep 2021 06:53:22 GMT
Connection: close
Content-Type: text/html; charset=us-ascii
Content-Length: 334
}
这是包含 RegisterAsync 方法的服务 class
public class UserService
{
private string androidApiUrl = "https://10.0.2.2:44358/api/Account/Register";
private string iOSApiUrl = "https://localhost:44358/api/Account/Register";
private HttpClient client;
public UserService()
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback =
(msg, cert, chain, err) => { return true; };
client = new HttpClient(httpClientHandler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<bool> RegisterAsync(string username, string pswd, string confmPswd)
{
var newAccount = new ModelAccount
{
Email = username,
Password = pswd,
ConfirmPassword = confmPswd
};
var json = JsonConvert.SerializeObject(newAccount);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")
{
CharSet = Encoding.UTF8.WebName
};
try
{
var response = await client.PostAsync(androidApiUrl, content);
Console.WriteLine(response.ToString());
Console.WriteLine(response.ReasonPhrase);
Console.WriteLine(response);
return response.IsSuccessStatusCode;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
}
}
这是 WebAPI 的属性...
这是 json 序列化的 ModelConvert。
class ModelAccount
{
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
如果我按照 this blog 的建议将端口号更改为 61902,如果有任何不同,它仍然无法工作。
谢谢
编辑:
按照@Aristos 的要求,这里是连接的端口号。
在设置中您已经定义了端口号 - 通常这是一个随机端口,因此您的系统可能已经更改。当你 运行 visual studio 时检查 端口你的本地 iis 是 运行ning 并根据
改变它
我正在开发一个使用 ASP.NET Web 应用程序模板的 Xamarin 表单应用程序。我 followed this tutorial 在 YouTube 上。我在使用 PostAsync 方法时遇到问题。 Web api 本身可以通过我本地计算机上的 IIS Chrome 正常启动,但不能通过模拟器启动。我打印了响应消息,显示为
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 26 Sep 2021 06:53:22 GMT
Connection: close
Content-Type: text/html; charset=us-ascii
Content-Length: 334
}
这是包含 RegisterAsync 方法的服务 class
public class UserService
{
private string androidApiUrl = "https://10.0.2.2:44358/api/Account/Register";
private string iOSApiUrl = "https://localhost:44358/api/Account/Register";
private HttpClient client;
public UserService()
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback =
(msg, cert, chain, err) => { return true; };
client = new HttpClient(httpClientHandler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<bool> RegisterAsync(string username, string pswd, string confmPswd)
{
var newAccount = new ModelAccount
{
Email = username,
Password = pswd,
ConfirmPassword = confmPswd
};
var json = JsonConvert.SerializeObject(newAccount);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")
{
CharSet = Encoding.UTF8.WebName
};
try
{
var response = await client.PostAsync(androidApiUrl, content);
Console.WriteLine(response.ToString());
Console.WriteLine(response.ReasonPhrase);
Console.WriteLine(response);
return response.IsSuccessStatusCode;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
}
}
这是 WebAPI 的属性...
这是 json 序列化的 ModelConvert。
class ModelAccount
{
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
如果我按照 this blog 的建议将端口号更改为 61902,如果有任何不同,它仍然无法工作。
谢谢
编辑:
按照@Aristos 的要求,这里是连接的端口号。
在设置中您已经定义了端口号 - 通常这是一个随机端口,因此您的系统可能已经更改。当你 运行 visual studio 时检查 端口你的本地 iis 是 运行ning 并根据
改变它