无法从移动应用程序连接到 api 端点 [英] Can't connect to api endpoint from mobile app

查看:39
本文介绍了无法从移动应用程序连接到 api 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 .NET Core web api 端点,我可以在其中检索我想要在我的移动应用程序中显示的一些图像.我通过导航到该路由并通过邮递员在本地测试了端点,并且两者都工作正常(显示正确的响应).但是,当我尝试从移动端访问端点时,它不起作用.我收到这个神秘的错误:

System.Net.Http.HttpRequestException:网络子系统关闭

以下是更多异常信息:

System.Net.Http.HttpRequestException:网络子系统关闭--->System.Net.Sockets.SocketException: 网络子系统关闭在 System.Net.Http.ConnectHelper.ConnectAsync(System.String 主机,System.Int32 端口,System.Threading.CancellationToken 取消令牌)[0x000c8] 在/Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs:65

我不确定我在移动端做错了什么.我知道邮递员要让它显示出来,我不得不关闭 SSL 认证设置.我需要在移动端做类似的事情吗?

这是我尝试调用端点的方法.我正在使用改装:

 公共静态类 FetchMediaService{公共静态 INevarroApi apiService;静态字符串 baseUrl = "https://localhost:5001";public static async Task>CallImagesEndpoint(){apiService = RestService.For<INevarroApi>(baseUrl);var 图像 = 等待 apiService.GetImages();返回图像;}}

地点:

 公共接口 INevarroApi{[获取(/图像")]任务<列表<Uri>>获取图像();}

OnAppearing in my codebehind for my view:

 protected override async void OnAppearing(){DualScreenLayoutInfo.PropertyChanged += OnFormsWindowPropertyChanged;DualScreenInfo.Current.PropertyChanged += OnFormsWindowPropertyChanged;var images = await FetchMediaService.CallImagesEndpoint();//抛出异常的地方}

我在上面做错了什么?

我正在 Mac OSX 上的 Android 10 (API 29) 模拟器上对此进行测试,后端代码使用 Visual Studio for Mac(后端是 .NET Core 3.1 web api 项目)在本地运行.

解决方案

如果您使用的是 IISExpress,请尝试更改您的调试配置.

对于 Kestrel

应通过 Program.cs 中的 IHostBuilder 进行设置,如下所示:

public static IHostBuilder CreateHostBuilder(string[] args){返回 Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{//这应该做webBuilder.UseUrls("http://127.0.0.1:5000");webBuilder.UseStartup();});}

I created a .NET Core web api endpoint where I can retrieve some images that I want to display in my mobile app. I tested the endpoint locally by navigating to that route and via postman and both are working fine (showing the proper response). However, when I try to hit the endpoint from mobile side, it doesn't work. I get this cryptic error:

System.Net.Http.HttpRequestException: Network subsystem is down

Here's more of the exception info:

System.Net.Http.HttpRequestException: Network subsystem is down ---> System.Net.Sockets.SocketException: Network subsystem is down
  at System.Net.Http.ConnectHelper.ConnectAsync (System.String host, System.Int32 port, System.Threading.CancellationToken cancellationToken) [0x000c8] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs:65

I'm not sure what I'm doing wrong on the mobile side. I know for postman to get it to show, I had to turn SSL certification setting off. Do I need to do something similar on the mobile side?

Here's how I'm trying to do the call to the endpoint. I'm using refit:

    public static class FetchMediaService
    {
        public static INevarroApi apiService;
        static string baseUrl = "https://localhost:5001";

        public static async Task<List<Uri>> CallImagesEndpoint()
        {
            apiService = RestService.For<INevarroApi>(baseUrl);
            var images = await apiService.GetImages();
            return images;
        }

    }

Where:

    public interface INevarroApi
    {
        [Get("/images")]
        Task<List<Uri>> GetImages();
    }

OnAppearing in my codebehind for one of my views:

        protected override async void OnAppearing()
        {
            DualScreenLayoutInfo.PropertyChanged += OnFormsWindowPropertyChanged;
            DualScreenInfo.Current.PropertyChanged += OnFormsWindowPropertyChanged;
            var images = await FetchMediaService.CallImagesEndpoint(); //Where the exception is thrown 

        }

What am I doing wrong above?

I'm testing this on Android 10 (API 29) emulator on Mac OSX and backend code is running locally using Visual Studio for Mac (backend is .NET Core 3.1 web api project).

解决方案

If you are using IISExpress try to change your debug configuration.

Change to http://localhost:[PORT] to http://127.0.0.1:[PORT]

Then you should be able to access it in android with http://10.0.2.2:[PORT]

For Kestrel

Settings should be done via IHostBuilder in Program.cs as following:

public static IHostBuilder CreateHostBuilder(string[] args)
{
    return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    // This should do it
                    webBuilder.UseUrls("http://127.0.0.1:5000");
                    webBuilder.UseStartup<Startup>();
                });
}

这篇关于无法从移动应用程序连接到 api 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆