'System.Net.WebException: Error: NameResolutionFailure when Calling WEBAPI in xamarin forms

I have created one simple xamarin form application using PCL, which is showing data in listview using WEBAPI. When i call WebAPI , i am getting below error.

ex = {System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00058] in <a547bd0d78184f26ab08d...

My WEBAPI:-

private const string WebServiceUrl = "http://servername/WEBAPI/api/employee/";

I am getting error on below code,

var json = await httpClient.GetStringAsync(WebServiceUrl);

If anybody faced same issue, Please help me. Thanks in Advance.

My RestClient Code:-

    public async Task<List<T>> GetAsync()
    {
        //var taskModels = new List<T>();
        var test = new List<T>();

        try
        {

            var httpClient = new HttpClient();

            var json = await httpClient.GetStringAsync(WebServiceUrl);

            var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

            test = taskModels;
        }

        catch(Exception ex)
        {
           
        }

        return test;
    }


Solution 1:[1]

Your domain is incorrect. Please supply a fully qualified domain name

Edit: assuming you have some config that maps server ip address to the domain(host file, private dns, etc) just replace msweb102to whatever the ip address of your server is

eg. 127.0.0.1/WEBAPI/api/employee/

Solution 2:[2]

Where do you got error on simulator on on real device.

In this case, it looks like that your test environment can not access the API environment

Change MSWeb02 to become IP address and make sure you are setup window firewall to allow outbound connection on port 80.

The easy way to test it work is open web api in the browser of the device. If that work, your code will work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ted Bigham
Solution 2 Truong Nguyen