Try Catch on HttpClient not working

why does HttpClient.GetAsync does throw exception when the server cannot be found? how can I handle this and tell the user that it cannot connect to the server. I tried adding the timeout but, it throws taskcancelexception (not sure if its the right word), but it give me "A task was canceled" exception error. If I used the timeout approach, I cannot tell if the server is down or it's just taking long to process the request.

btn1.Click += async (object sender, EventArgs e) =>
{
    try
    {
        using (var httpClient = new HttpClient())
        {
            var response = await httpClient .GetAsync("http://192.168.100.10:4200");
            if (response.IsSuccessStatusCode)
            {
                Toast.MakeText(this, "Works", ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(this, response.StatusCode.ToString(), ToastLength.Short).Show();
            }
        }
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.GetBaseException().Message, ToastLength.Short).Show();
    }
};
Tagged:

Best Answer

Answers

  • paulharley421paulharley421 USMember ✭✭

    bump, anyone?

  • NMackayNMackay GBInsider, University mod
    edited August 2017

    There's a few thing going on here that probably don't help, "Using" and HttpClient can have issues and calling the method in an anonymous delegate within an event handler probably swallows up the exception.

  • paulharley421paulharley421 USMember ✭✭

    @NMackay said:
    There's a few thing going on here that probably don't help, "Using" and HttpClient can have issues and calling the method in an anonymous delegate within an event handler probably swallows up the exception.

    any idea how to fix this? I tried it in console app, and it was working, the exception is being caught.

  • NMackayNMackay GBInsider, University mod

    @paulharley421

    I'll try and post an example tomorrow, please remind me if I forget as I've been extremely busy this last (1-2 months).

  • Gigex42Gigex42 USMember ✭✭✭✭
    edited August 2017

    This is what I always do without an exception.

    `using (HttpClient client = new HttpClient())
    {
    using (HttpResponseMessage response = await client.PostAsync(reqstring, cont))
    {
    if (response.StatusCode != System.Net.HttpStatusCode.OK)
    return null;

                        using (HttpContent content = response.Content)
                        {
                            string result = await content.ReadAsStringAsync();
                            var Settings = JsonConvert.DeserializeObject(result);
                            return Settings;
                        }
                    }
                }`
    
  • BjornBBjornB USMember ✭✭✭
    edited August 2017

    I would recommend just using one instance of HttpClient, see this link https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

  • paulharley421paulharley421 USMember ✭✭

    @Gigex42 said:
    This is what I always do without an exception.

    `using (HttpClient client = new HttpClient())
    {
    using (HttpResponseMessage response = await client.PostAsync(reqstring, cont))
    {
    if (response.StatusCode != System.Net.HttpStatusCode.OK)
    return null;

                        using (HttpContent content = response.Content)
                        {
                            string result = await content.ReadAsStringAsync();
                            var Settings = JsonConvert.DeserializeObject(result);
                            return Settings;
                        }
                    }
                }`
    

    app crashed when the server is invalid, is what should I do to inform the user that the server is down or wrong server ip when connecting

  • NMackayNMackay GBInsider, University mod

    @BjornB said:
    I would recommend just using ine instance of HttpClient, see this link https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

    Good advice, I went down the same route.

  • paulharley421paulharley421 USMember ✭✭

    bump, anyone?

Sign In or Register to comment.