Xamarin App无法在LocalHost上调用API

yb3bgrhw  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(174)

我在从Xamarin Android应用程序调用ASP.NET核心API时遇到问题。我在android模拟器上运行android应用程序,在本地机器上运行API。我从本地机器调用API没有问题,所以我知道它工作正常。我也可以从Xamarin应用程序调用其他API。我正在使用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在Postman上的样子:

isr3a4wc

isr3a4wc1#

我能够解决这个问题,多亏了这个问题: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();
        }
    }
}

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

相关问题