Ashok

Ashok

  • NA
  • 507
  • 76.4k

how to check web api is down or not in c# code

Dec 5 2019 1:22 AM
Hi,
 
Thanks in advance :)
  1. readonly string Baseurl = "http://localhost:123456/";  
  2. public string GetUserID_By_UserName(string Username)  
  3. {  
  4. string UserName = "";  
  5. using (HttpClient client = new HttpClient())  
  6. {  
  7. client.BaseAddress = new Uri(Baseurl);  
  8. client.DefaultRequestHeaders.Clear();  
  9. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  10. try  
  11. {  
  12. var response = client.GetAsync("api/Values/UserID_By_UserName?username=" + Username).Result;  
  13. // Here how i can check the api is working or not how to get the status to display of the api.  
  14. }  
  15. }  
  16. }  

Answers (10)