My workteam created a MVC Application integrated with an API for school project. This API is connected to n APIs.
I want to do HttpGet from the "main" API which will ask all APIs to make HttpGet in order to obtain lists from each one of them. I want this to work even if some APIs are offline but ... How can I check their internet connection through their Uri before making those Http requests? I tried to ping the API but I found some problems
public async Task<List<Booking>> GetAvailable(DateTime start, DateTime end)
{
var uriList = new List<string>();
var availableList = new List<Booking>();
var hotelList = await _repo.FindAllAsync();
foreach (var hotel in hotelList)
{
uriList.Add(hotel.Uri);
}
foreach (var uri in uriList)
{
if(ServerConnected(uri))
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
(...)
}
(...)
public static bool ServerConnected(string url)
{
Ping pingSender = new Ping();
var reply = pingSender.Send(url);
if (reply.Status == IPStatus.Success)
{
return true;
}
else
return false;
}