''System.Net.Http.HttpRequestException Connection Refused' Xamarin
I have my API that is running correctly, then I have my application in which I have:
public partial class MainPage : ContentPage
{
HttpClientHandler insecureHandler;
HttpClient client;
Uri uri = new Uri("http://172.20.10.2:3000/api/operatori/getoperatori");
public MainPage()
{
InitializeComponent();
insecureHandler = GetInsecureHandler();
client = new HttpClient(insecureHandler);
client.Timeout = TimeSpan.FromSeconds(200); // this is double the default
}
async void Button_Clicked(object sender, EventArgs e)
{
bool login = false;
try
{
Task<String> result = getOperation();
string result2 = await result;
var Item = JsonConvert.DeserializeObject<List<Operatore>>(result2);
foreach (Operatore o in Item)
{
if (o.oP_NOME == user.Text && o.oP_PASSWD == pass.Text)
{
login = true;
}
}
}
catch (Exception ex)
{
welcome.Text += ex.ToString();
}
if (login == true)
{
await Navigation.PushAsync(new HomePage(user.Text));
}
else
{
await DisplayAlert("Login fallito!", "Username o Password Errati", "OK");
}
}
private async Task<String> getOperation()
{
HttpResponseMessage response = await client.GetAsync(uri).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var risposta = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return risposta;
}
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;
}
}
172.20.10.2 is my IP address. If I launch Google Chrome on my PC (where API is running) it work, but when I try yo make this HTTP request on my Android device (not emulator) I have this error:
System.Net.Http.HttpRequestException Connection refused ---> System.Net.Sockets.SocketException: Connection refused
Even if I make the request from Chrome browser on phone, I have the same error.
Can I solve it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
