How to resolve "Unable to resolve host" error?

Viewed 2453

I am working on xamarin forms. Where I am consuming the API. When I am running the app in android 5 it is working fine but If I am running 7 + it is giving the error like

Unable to resolve host "almsdev.southeastasia.cloudapp.azure.com": No address associated with hostname.

How to resolve this?

Following is the code snippet:

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

On button click I am calling above method

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

My Base URL is
http://almsdev.southeastasia.cloudapp.azure.com:81/api/

I am using Http client = new Http();

2 Answers

Since you are targeting Android 9.0(API level 28) cleartext(non-HTTPS) support is disabled by default. To overcome this you can: either use HTTPS, or, add configs to allow http traffic.

First, in your Android Project, create a folder: xml and add the file 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>

Then, in your Manifest.xml

   <application android:label="YourAppName" android:networkSecurityConfig="@xml/network_security_config">

You can read more in the Xamarin Blog Post

Try to Add the login credentials to your Header Http Client.

Related