Android Emulator not consuming API and giving error System.Net.WebException: 'Failed to connect to

AustyAusty Member ✭✭✭
edited October 15 in Xamarin.Forms

l have an api backend in a .net core web app and have written the code to consume the api in xamarin.forms. when l run the code
l get the following error.

System.Net.WebException: 'Failed to connect to /127.0.0.1:44360'.

Below is my code in xamarin.forms

public MainPage()
{
InitializeComponent();
Comic();
}

    public async void Comic()
    {
        using(var httpClient = new HttpClient()){
            var response = await httpClient.GetStringAsync("https://127.0.0.1:44360/api/comic");
            var comic = JsonConvert.DeserializeObject<List<Comic>>(response);
            comicList.ItemsSource = comic;
        }
    }

A bit of research says something about configuring the web app to use 127.0.0.1 instead of localhost and it been
specific to android.what can l do to fix this error.

Best Answer

Answers

  • mjdevelopermjdeveloper Member ✭✭✭

    In your API solution dir ---> .vs/config ----> change ---> applicationhost.config

    <sites>
                <site name="YourAPI" id="3">
                    <application path="/" applicationPool="Clr4IntegratedAppPool">
                        <virtualDirectory path="/" physicalPath="G:\MyTfs\ShahvarWeb\ShahvarAPI" />
                    </application>
                    <bindings>
                        <binding protocol="http" bindingInformation="*:16907:localhost" />
                <binding protocol="http" bindingInformation="*:16907:*" />
                    </bindings>
                </site>
    

    you can set your post instead of 16907

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    Using 127.0.0.1 is incorrect.

    127.0.0.1 or localhost is always the current system. So in your case your code assumes that your backend is running on your android device.

    Instead of using 127.0.0.1 you will need to use the ip of your server where your backend is running.

  • AustyAusty Member ✭✭✭

    @DirkWilhelm Is the ip on IIS express on my local computer the ip of my computer i.e the ip l get when l run ipconfig

  • AustyAusty Member ✭✭✭

    @mjdeveloper after some research l think you are correct but how exactly do l get the applicationhost.config
    file for my project to make the changes you suggested. l can currently on file the default which is located at
    Document>IIS>config>applicationhost.config

  • JarvanJarvan Member, Xamarin Team Xamurai

    how exactly do l get the applicationhost.config file for my project to make the changes

    The location of the file is currently in the %windir%\system32\inetsrv\config directory. The document walks through all the sections about ApplicationHost.config, you can refer to it.

    Introduction to ApplicationHost.config:
    https://docs.microsoft.com/en-us/iis/get-started/planning-your-iis-architecture/introduction-to-applicationhostconfig

  • AustyAusty Member ✭✭✭
    edited October 23

    l have not been able to resolve this issue. l have been able to locate my application host config file and find the section for my site
    but l have not been able to get the correct setting for my android simulator to work. This is the section l edited in applicationhost.config file. My Ip after running ipconfig is 192.168.56.1.

     <site name="FINALKAHPROJECT" id="2">
                    <application path="/" applicationPool="Clr4IntegratedAppPool">
                        <virtualDirectory path="/" physicalPath="C:\Users\SBSC QA TEAM\source\repos\FINALKAHPROJECT\FINALKAHPROJECT\FINALKAHPROJECT" />
                    </application>
                    <bindings>
                        <binding protocol="http" bindingInformation="*:56745:localhost" />
                         <binding protocol="http" bindingInformation="*:8080:192.168.56.1" />
                    </bindings>
                </site>
    

    Then the code to consume the api in the xamarin forms project is

     public async void Comic()
            {
                using(var httpClient = new HttpClient()){
                    var response = await httpClient.GetStringAsync("http://192.168.56.1:80/api/comic");
                    var comic = JsonConvert.DeserializeObject<List<Comic>>(response);
                    comicList.ItemsSource = comic;
                }
            }
    

    System.Net.WebException: 'Failed to connect to /192.168.56.1:80'.Help Please

  • AustyAusty Member ✭✭✭

    @Jarvan l have made the correction in applicationhost.config . will l use 127.0.0.1/api/comics in the xamarin code

  • AustyAusty Member ✭✭✭

    @Jarvan this is the exact error l am getting

    System.Net.WebException: 'Failed to connect to /127.0.0.1:80'

  • AustyAusty Member ✭✭✭

    @Jarvan it worked thanks.. l followed exactly what you said,l replaced localhost with 127.0.0.1 in the applicationhost.config file and used 10.0.0.2:port number in xamarin mobile to call the api..thanks alot..Love this forum and the people in it.

Sign In or Register to comment.