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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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;
}
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