Xamarin App 无法调用 LocalHost 上的 API

Xamarin App cannot call APIs on LocalHost

我在从 Xamarin Android 应用调用 ASP.NET 核心 API 时遇到问题。 我是 运行 android 模拟器上的 android 应用程序,我是 运行 我本地机器上的 API 应用程序。我可以毫不费力地从我的本地机器调用 api,所以我知道它可以工作。我还可以从 Xamarin 应用程序调用其他 apis。我使用 IP 地址 10.0.2.2 路由到我的本地机器。但是我无法从我的本地计算机成功地对 API 运行 进行任何 API 调用。

这是我的 MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="APICallerTest.MainPage">
    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="API Caller Test" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <StackLayout Padding="30,24,30,0">
            <Button x:Name="CallAPIButton" VerticalOptions="Center" Text="Call API" Clicked="CallAPIButton_Clicked"/>
            <Label x:Name="CalledLabel"></Label>
            <Label x:Name="ResultLabel"></Label>
            <Label x:Name="ErrorLabel"></Label>
            <Label x:Name="StackTraceLabel"></Label>
        </StackLayout>
    </StackLayout>
</ContentPage>

这是我的 MainPage.xaml.cs:

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace APICallerTest
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private async Task CallAPI()
        {
            ResultLabel.Text = "";
            ErrorLabel.Text = "";
            StackTraceLabel.Text = "";

            try
            {
                string uri = "http://10.0.2.2:57817/Weatherforecast";

                HttpClient httpClient = new HttpClient();
                HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);

                if (httpResponse.IsSuccessStatusCode)
                {
                    string result = await httpResponse.Content.ReadAsStringAsync();
                    ResultLabel.Text = $"Result: {result}";
                }
                else
                {
                    ResultLabel.Text = "Result: failed";
                }
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = $"Error Message: {ex.Message}";
                StackTraceLabel.Text = $"Stack Trace: {ex.StackTrace}";
            }
        }

        private void CallAPIButton_Clicked(object sender, EventArgs e)
        {
            CalledLabel.Text = "Called";
            CallAPI();
        }
    }
}

这是我的 api 在邮递员上的样子:

由于这个问题,我能够解决这个问题: The SSL connection could not be established

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace APICallerTest
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private async Task CallAPI()
        {
            ResultLabel.Text = "";
            ErrorLabel.Text = "";
            StackTraceLabel.Text = "";

            try
            {
                string uri = "http://10.0.2.2:57817/Weatherforecast";
                
                //++++++++++Added client handler here ++++++++++
                HttpClientHandler clientHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
                };
                HttpClient httpClient = new HttpClient(clientHandler);
                               

                HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);

                if (httpResponse.IsSuccessStatusCode)
                {
                    string result = await httpResponse.Content.ReadAsStringAsync();
                    ResultLabel.Text = $"Result: {result}";
                }
                else
                {
                    ResultLabel.Text = "Result: failed";
                }
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = $"Error Message: {ex.Message}";
                StackTraceLabel.Text = $"Stack Trace: {ex.StackTrace}";
            }
        }

        private void CallAPIButton_Clicked(object sender, EventArgs e)
        {
            CalledLabel.Text = "Called";
            CallAPI();
        }
    }
}

不过,一位用户指出,这确实会使您面临可能的安全风险。我认为它应该可以用于测试目的。