Xamarin Android application is not able to connect to MVC core web Api

Roshan Josey 0 Reputation points
2023-06-22T09:39:37.4866667+00:00

I am using MVC core web API for my Xamarin Android App to get http responses and my configuration is as follows:

Visual Studio version – 2022 Community edition Android Emulator is configured to run Android 13.

The Issue I am facing is that the Android application is throwing an exception while connecting with MVC core web API.

And here is the error that I got:

System.AggregateException: 'One or more errors occurred. (No such host is known)'

After debugging the code below IpSettings

I get the input value's in the code behind but it is showing an exception at "HttpResponseMessage resp = msg.Result;". I expect the Xamarin Android Api call to enter the MVC core web API controller code.

Here is the code that I used in code behind of a button click:

private async void btnRegisterClicked(object sender, EventArgs e)
    {
        HttpClientHandler insecureHandler = GetInsecureHandler();
        HttpClient httpClient = new HttpClient(insecureHandler);
        AppUserRegistrationModel userReg;
        AppUserRegistrationModel user = new AppUserRegistrationModel
        {
            user_fullname = entName.Text,
            user_email = entEmail.Text,
            phone_number = long.Parse(entPhoneNumber.Text),
            alternative_number = long.Parse(entPhoneNumber.Text),
            address = entAddress.Text,
            city = entCity.Text,
            district = entDistrict.Text,
            state = entState.Text,
            pincode = Int32.Parse(entPinCode.Text),
            gender_id = 1,
            aadhar_number = long.Parse(entAadharNumber.Text),
            dob = dtDOB.Date,
            blood_group_id = 2,
            registration_time = DateTime.Now,
            username = entUserName.Text,
            password = entPassword.Text,
            is_user_active = true
        };
        var userPostData = JsonConvert.SerializeObject(user);
        var buffer = System.Text.Encoding.UTF8.GetBytes(userPostData);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    Task<HttpResponseMessage> msg =   httpClient.PostAsync("http://10.0.2.2:7137/api/HospitalCategoryModels", byteContent); 

        HttpResponseMessage resp = msg.Result;
        string str;
        if (resp.IsSuccessStatusCode)
        {
            Task<string> strs = resp.Content.ReadAsStringAsync();
            userReg = JsonConvert.DeserializeObject<AppUserRegistrationModel>(strs.Result);
        }
        await Navigation.PushModalAsync(new MainPage());

    }
    
    public HttpClientHandler GetInsecureHandler()
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
        {
            if (cert.Issuer.Equals("CN=localhost"))
                return true;
            return errors == System.Net.Security.SslPolicyErrors.None;
        };
        return handler;
    }
Developer technologies .NET Xamarin
Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-06-22T15:05:15+00:00

    Android does not support http or self signed certificates by default. Also unlike windows, code can not override the setting. You need to

    update the manifest

    https://developer.android.com/training/articles/security-config

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.