获取NameResolutionFailure错误

时间:2016-06-10 09:27:19

标签: xamarin.android httpclient xamarin.forms

无法在Xamarin.Forms项目中获取数据。我尝试使用以下代码并且出现 NameResolutionFailure 错误。

     private const string BaseUrl = "http://intilaqemployees.azurewebsites.net/api/employeesapi";
            public async Task<List<Employee>> GetEmployeesAsync()
            {
                var httpClient = new HttpClient();
                try
                {
                    var jsonResponse = await httpClient.GetStringAsync(BaseUrl).ConfigureAwait(false);

                    //The following line never gets executed
                    var employeesList = JsonConvert.DeserializeObject<List<Employee>>(jsonResponse);

                    return employeesList;
                }
                catch (AggregateException exception) { }
                catch (Exception ex)
                {
                }

                return null;
            }

这是我到目前为止所尝试的

  1. 在Android清单中启用INTERNET
  2. 将主机名翻译为ip
  3. 尝试通过设置client.DefaultRequestHeaders.Host =“intilaqemployees.azurewebsites.net”直接设置主机;
  4. 在模拟器中关闭wifi
  5. 请注意:Android模拟器没有任何互联网连接。

1 个答案:

答案 0 :(得分:1)

这个代码解决了我的问题:

var client = new HttpClient {
    BaseAddress = new Uri("http://1.2.3.4"),
    DefaultRequestHeaders = { Host = "example.com" }
};