Last active
June 27, 2017 09:17
-
-
Save ginomessmer/a7c7a576bf12057b35ebd3ee305e94c0 to your computer and use it in GitHub Desktop.
HTTP Alive - Checks whether a website is up and running or nuked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using static System.Console; | |
namespace HttpAlive | |
{ | |
class Program | |
{ | |
static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); | |
public async Task MainAsync() | |
{ | |
WriteLine("Hey you! Enter the URL of the website you want to check and I'll start working"); | |
var url = new Uri(ReadLine()); | |
WriteLine("Looking good, I'm checking the site every 5 seconds now"); | |
while (true) | |
{ | |
ResetColor(); | |
await Task.Delay(5000); | |
HttpClient client = new HttpClient(); | |
client.Timeout = TimeSpan.FromSeconds(20); | |
WriteLine($"{DateTime.Now.ToString()} - Sending request..."); | |
HttpResponseMessage response = null; | |
try | |
{ | |
response = await client.GetAsync(url); | |
} | |
catch (TaskCanceledException ex) | |
{ | |
ForegroundColor = ConsoleColor.Red; | |
WriteLine($"{DateTime.Now.ToString()} - Timeout"); | |
continue; | |
} | |
if (response.IsSuccessStatusCode) | |
{ | |
ForegroundColor = ConsoleColor.Green; | |
WriteLine($"{DateTime.Now.ToString()} - Website is up and running"); | |
} | |
else | |
{ | |
ForegroundColor = ConsoleColor.Red; | |
WriteLine($"{DateTime.Now.ToString()} - Website is down ({response.StatusCode})"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment