HttpClient.GetAsync request fails with NameResolutionFailure

PhilipMurrayPhilipMurray ✭✭GBMember ✭✭

I am taking a first look at Android development and I am having problems in making requests out to Http endpoints. In this test case I am the Northwind OData API.

Now I can make a request from my local browser and by running the below code from a console app in Visual Studio but when I run the code in the Google Android emulator on my desktop it fails with a WebException - NameResolutionFailed.

The interesting thing is that I can use the local browser to hit the endpoint and the Json is returned. Thinking that this may be a permissions issue I check the android app manaifest in Xamarin Studio and I have the required permissions of AccessNetworkState and Internet set.

This is not limited to this endpoint as I have tried others and get the same issue.

using Cirrious.MvvmCross.ViewModels;
using System.Collections.Generic;
using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using ModernHttpClient;

    namespace Mobile.Core.ViewModels
    {
        public class WelcomeViewModel : MvxViewModel
        {
            private IList<Course> courses;

            public string Welcome 
            {
                get{ return "Members Welcome"; }
            }

            public IList<Course> Courses 
            { 
                get
                {
                    return courses;
                }
                set
                {
                    courses = value;
                    RaisePropertyChanged(() => Courses);
                }
            }

            public WelcomeViewModel()
            { 
                GetData();
            }

            private async Task GetData()
            {
                try 
                {
                    using (HttpClient client = new HttpClient(new NativeMessageHandler()) {BaseAddress = new Uri("http://services.odata.org/V4/Northwind/Northwind.svc/")}) 
                    {
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        HttpResponseMessage responseMessage = await client.GetAsync("Summary_of_Sales_by_Year", HttpCompletionOption.ResponseContentRead);

                    }
                } 
                catch (Exception ex) 
                {
                    System.Diagnostics.Debugger.Break ();
                }

            }
        }
    }

Thinking that the emulator may not connect correctly I ran the below cose and the ActiveNetWorkInfo is showing as Online whereas the wireless is showing offline which is what I would expect.

    Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager) GetSystemService(ConnectivityService);

    Android.Net.NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;
    bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

    Android.Net.NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(Android.Net.ConnectivityType.Wifi);
    if(wifiInfo.IsConnected)
    {
        Android.Util.Log.Debug("", "Wifi connected.");
    } else
    {
        Android.Util.Log.Debug("", "Wifi disconnected.");
    }

Finally. I am on a corporate LAN which may be causing DNS issues.

Best Answer

Answers

Sign In or Register to comment.