Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
whenever PostAsJsonAsync api failed due to internet disconnect at the time catch block didn't catch the exception. I want to catch the exception if internet disconnected

C#
try
{
Data cus = new Data { Searchterm = searchterm };                    
  response = await  httpclient.PostAsJsonAsync(request.RequestUri,cus);               
  If(response.IsSuccessStatusCode)
   {                   
 var customerresult = await response.Content.ReadFromJsonAsync<customerresponse>();
 customers= customerresult.BaseObjects.ToList();
    }
}
catch (HttpRequestException ex)
{
         navigationmanager.NavigateTo("\page");
 }







I want to catch the exception if internet disconnectedplese help me
thanks

What I have tried:

I want to catch the exception if internet disconnected
Posted
Updated 24-Jan-23 14:23pm
v3
Comments
Richard MacCutchan 24-Jan-23 12:15pm    
Maybe the call is not throwing an exception. You can check by removing the try/catch and checking what happens.
Ramesh p 24-Jan-23 12:27pm    
ERR_INTERNET_DISCONNECTED shows on browser console. we need to handle. previsuly I had handle for external external api failed due to internetdiconnected. I had used try and catch for handle
Richard MacCutchan 24-Jan-23 12:31pm    
That is an error response, not an exception. So you need to add an else clause to your if block to handle unsuccessful return values.
Ramesh p 24-Jan-23 13:01pm    
initally i had handle for external api same like required web api
Richard MacCutchan 24-Jan-23 14:15pm    
I have no idea what that means. But look at your code:
  If(response.IsSuccessStatusCode)
   {                   
 var customerresult = await response.Content.ReadFromJsonAsync<customerresponse>();
 customers= customerresult.BaseObjects.ToList();
    }

Just add below it:
else
{
    // handle non-success return codes here
    var status = response.StatusCode;
    // display its value
}

1 solution

Rather than checking response.IsStatusCode, use response.EnsureSuccessStatusCode();. What this does is throw an exception if the status code indicates that the response was not successful. The good news about this is it validates far more than whether or not the network connection was disconnected during the call.
 
Share this answer
 
Comments
Ramesh p 25-Jan-23 4:59am    
Actually while call the api at this below line when the internet disconnected at the time needs to catch the exception on catch block but how response.isstatscode excuted

response = await httpclient.PostAsJsonAsync(request.RequestUri,cus);
Pete O'Hanlon 25-Jan-23 8:17am    
A disconnect does not trigger an exception - it's just an error code. What you need to wait for, is for the async call to time out. Once you get the timeout, EnsureSuccessStatusCode() will trigger an exception. If you want to reduce the timeout on your HttpClient, you can specify that as an option. I believe the default is 100 seconds.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900