1

I've been trying to perform a GET request from Visual Studio's Android Emulator to an ASP.Net Core API running on localhost. I first read that I have to use IP 10.0.2.2 when performing the request from Android, so I have the following HTTP client code in my Android (Xamarin) App

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            var logInbutton = FindViewById<Button>(Resource.Id.logInButton);

            logInbutton.Click += OnLogInButtonClicked;
        }

        private async void OnLogInButtonClicked(object sender, EventArgs e)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                var uri = new Uri("http://10.0.2.2:5555/api/Orders?api-version=0.9");
                var response = await client.GetAsync(uri);
            }
        }

However, each time I attempt to run this I'm greeted with a 400 Bad Request error. I know the request is okay since I can run the same Http client code from a console app without any errors. I then read that IIS express does not accept external requests and since the Android emulator is running in a VM this could be the reason.

So I then followed the advice on this SO post but was greeted with an Invalid URI: The hostname could not be parsed error. I'm not sure how to proceed from here. Is there a solution to the Invalid URI: The hostname could not be parsed error, or should I attempt to circumvent IIS Express and run solely with kestrel for development? Would even running solely in kestrel fix this problem?

James B
  • 8,975
  • 13
  • 45
  • 83
  • The often quoted solution is to change config files, but you can avoid that with our free VS Extension called Conveyor, https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti – Jim W Apr 12 '18 at 19:46

2 Answers2

0

I had a similar issue.

First, let's locate the correct applicationhost.config file. You should check this location:

[solution folder]/.vs/config/applicationhost.config

Then people advice to set up binding like this:

<binding protocol="http" bindingInformation="*:5555:*" />

It did NOT work for me. I received "Invalid URI: The hostname could not be parsed".

What did work for me is the syntax like this:

<binding protocol="http" bindingInformation=":5555:" />

Please note, port 5555 is given as example. You should use the port your server is listening to.

VeganHunter
  • 5,584
  • 2
  • 26
  • 26
0

I ran into the same issue when attempting to connect while running through kestrel. For me it worked using this configuration for the launchsettings:

launchsettings.json

Dbl
  • 5,634
  • 3
  • 41
  • 66