无法从移动应用连接到api端点

问题描述 投票:0回答:1

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

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

这里有更多异常信息:

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
  --- End of inner exception stack trace ---
  at System.Net.Http.ConnectHelper.ConnectAsync (System.String host, System.Int32 port, System.Threading.CancellationToken cancellationToken) [0x00180] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs:84
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () [0x0001b] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813
  at System.Net.Http.HttpConnectionPool.CreateConnectionAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x000ea] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:371
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () [0x0001b] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813
  at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync (System.Threading.Tasks.ValueTask`1[TResult] creationTask) [0x000a2] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:529
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () [0x0001b] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813
  at System.Net.Http.HttpConnectionPool.SendWithRetryAsync (System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x0003f] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:284
  at System.Net.Http.RedirectHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00070] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:32
  at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:531
  at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0`2[T,TBody].<BuildCancellableTaskFuncForMethod>b__0 (System.Net.Http.HttpClient client, System.Threading.CancellationToken ct, System.Object[] paramList) [0x00188] in <cda9777f03ee4e9188064495e9f2e568>:0
  at Nevarro.Services.FetchMediaService.CallImagesEndpoint () [0x0002e] in /Users/saramorsi/Projects/Nevarro/Nevarro/Services/FetchMediaService.cs:18
  at Nevarro.TwoPage.OnAppearing () [0x00058] in /Users/saramorsi/Projects/Nevarro/Nevarro/TwoPage.xaml.cs:41
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
  at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <f136c45b4a184119b54f2647ad4225f6>:0
  at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <f136c45b4a184119b54f2647ad4225f6>:0
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <f136c45b4a184119b54f2647ad4225f6>:0
  at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.34(intptr,intptr)

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

这是我尝试拨打到端点的电话。我正在使用改装:

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

    }

位置:

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

在我的代码背后出现在我的其中一种视图中:

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

        }

上面我在做什么错?

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

c# xamarin xamarin.forms xamarin.android refit
1个回答
0
投票

如果使用IISExpress,请尝试更改调试配置。

更改为http://localhost:[PORT]http://127.0.0.1:[PORT]

然后您应该可以使用[http://10.0.2.2:[PORT]

在android中访问它

Vs Settings

© www.soinside.com 2019 - 2024. All rights reserved.