2

Every time I tried to consume my web Api with my android emulator, It doesn't work.

I tried the simple get values but It didn't work :

    public async void Test()
        {

            HttpClient httpClient = this.HttplicentAccount;

            var uploadServiceBaseAdress = "https://192.168.0.19:44336/api/values";

            var value = await httpClient.GetAsync(uploadServiceBaseAdress);

            var status = value.StatusCode;

            Console.WriteLine(status);
        }

I use :

private HttpClient _httpClient;
        public HttpClient HttplicentAccount
        {
            get
            {
                _httpClient = _httpClient ?? new HttpClient
                (
                    new HttpClientHandler()
                    {
                        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
                        {
                    //bypass
                    return true;
                        },
                    }
                    , false
                )
                {
                    BaseAddress = new Uri("https://192.168.0.19:44336"),
                };

                // In case you need to send an auth token...
                //_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "YOUR_TOKEN");
                return _httpClient;
            }
        }

To Counter the certficate error.

I tried on swagger and the get values was working. But for example When I am starting my android emulator to test my web api is stuck, don't work.

guiz
  • 99
  • 1
  • 7
  • 1
    can you call your API from the web browser of the device/emulator? Have you checked the server logs to see why it is responding with. a 400? – Jason Jan 31 '20 at 21:36
  • 1
    Does this answer your question? [How to get the Android Emulator's IP address?](https://stackoverflow.com/questions/1720346/how-to-get-the-android-emulators-ip-address) – JSteward Jan 31 '20 at 21:48

1 Answers1

2

Nevermind, I fixed the issues : https://learn.microsoft.com/fr-fr/xamarin/cross-platform/deploy-test/connect-to-local-web-services

I created a new webApi project without https but it must be the same thing with https. Solution : I should write http://10.0.2.2:"port" instead of my previous ip, to connect with my wep Api Server in localhost. Ty for your help.

guiz
  • 99
  • 1
  • 7