I want to call an external Api in my application and after calling the Api if a timeout exception throws then my app returns a result. Now I want to simulate this situation. this is my code :
public string Sample()
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response =
client.GetAsync("ExternalApi").GetAwaiter().GetResult();
//...
}
}
catch (Exception e)
{
return ((HttpRequestException) e).StatusCode == HttpStatusCode.RequestTimeout ? "timeOut" : "Error";
}
return "Success";
}
If I throw a TimeoutException in ExternalApi then in Sample method I get Internal server error. How to create an artificial timeout exception?