0

I'm using C# .Net to call a web service asynchronously and would like to add a timeout. I cannot figure out how to catch the timeout exception correctly.

I'd rather not use the generic Exception since it's very broad. Preference is to catch the timeout exception specifically and alert the user of the timeout.

When a timeout occurs, if I print out the exception type, I get System.Net.WebException.

Is there a better way to catch the timeout exception?

        HttpResponseMessage response = new HttpResponseMessage();

        HttpClient client = new HttpClient();
        client.Timeout = new TimeSpan(1);

        client.BaseAddress = new Uri(dimURL);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //Response Code
        try
        {
            response = client.GetAsync(dimURL).Result;
        }
        catch (<CATCH TIMEOUT>)
        {

        }
        catch (Exception e)
        { }
HShhanick
  • 481
  • 1
  • 6
  • 14
  • 1
    Possible duplicate of [What exception is thrown if a web service I'm using times out?](http://stackoverflow.com/questions/2470933/what-exception-is-thrown-if-a-web-service-im-using-times-out) – Backs Jan 13 '17 at 02:02

1 Answers1

0

You are looking for the WebException. Here is the MSDN page for this.

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73