Xamarin 表单 ASP.NET android 模拟器无法访问 localhost API

约翰多

我正在开发一个使用 ASP.NET Web 应用程序模板的 Xamarin 表单应用程序。在 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 方法的服务类

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; }
    }

如果我按照本博客的建议将端口号更改为 61902 会有所不同,它仍然无法正常工作。

谢谢

编辑:根据@Aristos 的要求,这里是连接的端口号。 在此处输入图片说明

阿里斯托斯

在设置中,您定义了端口号 - 通常这是一个随机端口,因此您的系统可能会发生变化。运行visual studio时检查本地iis正在运行的女巫端口并根据情况进行更改。

设置

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android模拟器中连接localhost?

Ionic Framework:Android模拟器无法访问主机上的服务

Xamarin Android Player无法运行API 21或更高版本的模拟器

Xamarin表单-面向.NET标准

获取调用运行Localhost的ASP.NET Web APi的Xamarin Android设备

从Android Studio中的模拟器访问Localhost和Virtualhost?

如何从Android模拟器访问localhost?

Android模拟器未连接到localhost API

在Android模拟器上连接到localhost

当我运行Xamarin表单时,为什么我的Android模拟器是黑色的?

使用Xamarin将数据发布到ASP.NET API

Android模拟器浏览器无法访问在主机文件上设置的localhost子域

Asp.Net Core SignalR无法从Android Studio模拟器客户端连接

Xamarin表单:存档Android应用程序后,从Xamarin表单发布时,API始终接收空数据

xamarin android模拟器无法启动

显示api数据(Xamarin表单)

Xamarin App无法在LocalHost上调用API

在Android中访问Asp.net MVC Web Api

ASP.net MVC Web API + Android

Xamarin iOS模拟器无法连接到Rest API

访问localhost,Mac Android Studio模拟器

asp.net MVC表单POST在Localhost中有效,但发布后不起作用

无法在Xamarin VS中选择android模拟器

通过 FireBase 触发从 ASP.Net Api 到 Xamarin.Android 应用程序的推送通知

如何从服务器端(asp.net web api / mvc)向 xamarin 表单应用程序发送请求?

ASP.NET - 无法设置表单样式

从 Android 模拟器连接到本地 asp.net web 服务器

Xamarin Mobile 使用的 ASP.Net Core Web API

在 ASP.NET Core 中添加 IHostedService 服务时无法访问 API