如何解决“无法解析主机”错误?

Sag*_*til 6 xamarin.android asp.net-web-api xamarin xamarin.forms

我正在研究 xamarin 表单。我在哪里使用 API。当我在 android 5 中运行该应用程序时,它工作正常,但如果我运行 7 +,它会给出类似的错误

无法解析主机“almsdev.southeastasia.cloudapp.azure.com”:没有与主机名关联的地址。

如何解决这个问题?

以下是代码片段:

public async Task<HttpResponseMessage> Login(string EmailOrMobile)
    {
        try
        {

            var responseTask = await client.GetAsync(OauthBaseUrl + "OAuthSAAS/MSLogin?UserCredentials=" + EmailOrMobile);
            return responseTask;
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

单击按钮时我正在调用上面的方法

 private async void fn_Submit_Clicked(object sender, EventArgs e)
 {  
    var response = await _oauthService.Login(txt_credentails.Text.Trim()); 
 }
Run Code Online (Sandbox Code Playgroud)

我的基本 URL 是
http://almsdev.southeastasia.cloudapp.azure.com:81/api/

我正在使用 Http client = new Http();

Bru*_*iro 1

由于您的目标是 Android 9.0(API 级别 28),默认情况下禁用明文(非 HTTPS)支持。要克服这个问题,您可以:使用 HTTPS,或者添加配置以允许 http 流量。

首先,在您的 Android 项目中,创建一个文件夹:xml 并添加文件network_security_config

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">almsdev.southeastasia.cloudapp.azure.com</domain>
  </domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)

然后,在你的Manifest.xml

   <application android:label="YourAppName" android:networkSecurityConfig="@xml/network_security_config">
Run Code Online (Sandbox Code Playgroud)

您可以在Xamarin 博客文章中阅读更多内容