I got the following:
public async Task<bool> IsAuthenticatedAsync()
{
using (HttpClient client = new HttpClient())
{
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
content.Add(new StringContent(this.Username, Encoding.UTF8), "username");
content.Add(new StringContent(this.Password, Encoding.UTF8), "password");
try
{
HttpResponseMessage message = await client.PostAsync(authenticatedUrl, content);
if (message.StatusCode == HttpStatusCode.Accepted)
return true;
return false;
}
catch(HttpRequestException)
{
System.Windows.Forms.MessageBox.Show("Some text.");
}
}
}
return false;
}
where authenticatedUrl is some static Uri.
Now assuming that the webserver is not available (or the adress is wrong) await client.PostAsync(authenticatedUrl, content) throws a HttpRequestException, hence the try-catch.
Problem is that the exception is not getting caught. I tried turning off Just My Code but that just adds other exceptions (i.e. SocketException) as suggested here and still doesn't let catch handle the exception.
Why isn't the exception being caught?
Edit
main form(GUI):
public GUI(...)
{
...
CheckLoginState(username, password);
...
}
private async void CheckLoginState(string username, string password)
{
User user = new User(username, password);
if (user.Username != null && user.Password != null && await user.IsAuthenticatedAsync())
abmeldenToolStripMenuItem.Enabled = true;
}