0

My scenario is as follows. I have an Android Emulator which is hosting an EmbedIO web server through an App. When I try to access the URL to the web server from the host machine's (Mac) browser I receive ERR_EMPTY_RESPONSE error.

I have issued the following port forwarding commands through ADB: adb forward tcp:8080 tcp:8080

In the browser I am navigating to: http://localhost:8080/api/ChangeBackGround and the Android emulator web server is listening on: http://10.0.2.16:8080/api/ChangeBackGround

Here is the code that starts the web server in the Xamarin Forms App (runs on Android Emulator):

public static class WebServerFactory
{
    public static WebServer CreateWebServer<T>(string url, string baseRoute)
        where T : WebApiController, new()
    {
        var server = new WebServer(url)
            .WithWebApi(baseRoute, api => api.WithController<T>());

        // Listen for state changes.
        server.StateChanged += (s, e) => Debug.WriteLine($"WebServer New State - {e.NewState}");

        return server;
    }
}

public class EventController : WebApiController
{
    [Route(HttpVerbs.Get, "/ChangeBackGround")]
    public void ChangeBackGround()
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            App.Current.MainPage.BackgroundColor = Color.Green;
        });
    }
}

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    private WebServer _WebServer;

    protected override void OnStart()
    {
        var localIPAddress = GetLocalIPAddress();
        var url = $"http://{localIPAddress}:8080";

        Task.Factory.StartNew(async () =>
        {
            _WebServer = WebServerFactory.CreateWebServer<EventController>(url, "/api");
            await _WebServer.RunAsync();
        });

        ((MainPage)MainPage).Url = url;
    }

    private string GetLocalIPAddress()
    {
        var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();

        if (IpAddress != null)
            return IpAddress.ToString();

        throw new Exception("Could not locate IP Address");
    }

    protected override void OnSleep()
    {
    }

    protected override void OnResume()
    {
    }
}

The scenario currently works on the iOS simulator and an Android physical device. But I always get ERR_EMPTY_RESPONSE even when I've setup the port forwarding rules.

Any help would be much appreciated.

Sach K
  • 591
  • 4
  • 20

2 Answers2

0

Please first make sure your android emulator could connect internet properly.

If the problem perisist, you can try the following methods:

Method 1:

1.start your Command prompt by runing as an admistrator;

2.Run the following commands in sequence:

netsh int ip reset c:\resetlog.txt

netsh winsock reset

ipconfig /flushdns

exit

Method 2: (If method 1 didn't work,try modthod 2)

Open your Control Panel -->NetWork and Internet-->Network and Sharing Center-->Change adapter settings-->right click Ethenet and click Properties-->select Internet Protocol 4-->click Properties -->using the following NDS server addresses

Fill in the following configuration:

Preferred NDS Server: 1.1.1.1

Alternate NDS Server: 1.0.0.1

Method 3: (If above methods didn't work,try modthod 3)

Open Settings in your PC-->Open NetWork and Internet-->click Nnetwork reset-->press Reset Now

Note:

For more details, you can enter keywords How to fix "ERR_EMPTY_RESPONSE" Error [2021] in your browser and then you will find relative tutorail.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
0

You should have you service listening on either one of these IPs:

  • 127.0.0.1: The emulated device loopback interface (preferred, I don't think you have reasons to use a different one)
  • 10.0.2.15: The emulated device network/ethernet interface

See https://developer.android.com/studio/run/emulator-networking#networkaddresses

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134