Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Various fixes and formatting #2

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 53 additions & 36 deletions HttpPing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ namespace web_ping
class Program
{
// Properties
public static bool Infinite = false;
public static bool Detailed = false;
public static bool Timestamp = false;
public static bool NoColor = false;
public static bool UseCommonLogFormat = false;
public static int Interval = 30000;
public static int Requests = 5;
private static bool Infinite { get; set; } = false;
private static bool Detailed { get; set; } = false;
private static bool Timestamp { get; set; } = false;
private static bool NoColor { get; set; } = false;
private static bool UseCommonLogFormat { get; set; } = false;
private static int Interval { get; set; } = 30000;
private static int Requests { get; set; } = 5;

// Console Properties
public static ConsoleColor DefaultBackgroundColor;
public static ConsoleColor DefaultForegroundColor;
private static ConsoleColor DefaultBackgroundColor { get; set; }
private static ConsoleColor DefaultForegroundColor { get; set; }

// Constants
public const string USAGE = "USAGE: Requester web_address [-d] [-t] [-ts] [-n count] [-i interval] [-l] [-nc]";
private const string Usage = "USAGE: Requester web_address [-d] [-t] [-ts] [-n count] [-i interval] [-l] [-nc]";

private static string resolvedAddress = "";

static void Main(string[] args)
private static void Main(string[] args)
{
// Arguments check
if (args.Length == 0)
{
Console.Write(USAGE);
Console.WriteLine(Usage);
Environment.Exit(0);
}

Expand All @@ -40,7 +40,7 @@ static void Main(string[] args)
DefaultForegroundColor = Console.ForegroundColor;

// Parse arguments
try
try
{
for (int count = 0; count < args.Length; count++)
{
Expand All @@ -50,7 +50,7 @@ static void Main(string[] args)
case "-?":
case "--?":
case "/?":
Console.WriteLine(USAGE);
Console.WriteLine(Usage);
Environment.Exit(0);
break;
case "-i":
Expand Down Expand Up @@ -98,25 +98,31 @@ static void Main(string[] args)
catch (IndexOutOfRangeException)
{
Error("Missing argument parameter");
Console.WriteLine(USAGE);
Console.WriteLine(Usage);
Environment.Exit(1);
}
catch (ArgumentException)
{
Error("Incorrect argument found");
Console.WriteLine(USAGE);
Console.WriteLine(Usage);
Environment.Exit(1);
}
catch (FormatException)
{
Error("Could not convert argument");
Console.WriteLine(USAGE);
Console.WriteLine(Usage);
Environment.Exit(1);
}
catch (OverflowException)
{
Error("Could not convert argument");
Console.WriteLine(Usage);
Environment.Exit(1);
}
catch (Exception e)
{
Error(e.GetType().ToString());
Console.WriteLine(USAGE);
Console.WriteLine(Usage);
Environment.Exit(1);
}

Expand Down Expand Up @@ -146,7 +152,7 @@ static void Main(string[] args)
}

// SO: https://stackoverflow.com/questions/27108264/c-sharp-how-to-properly-make-a-http-web-get-request
static void HttpRequestLoop(string query)
private static void HttpRequestLoop(string query)
{
// Construct request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
Expand All @@ -157,28 +163,39 @@ static void HttpRequestLoop(string query)
// Send requests
int index = 0;
Console.WriteLine("Sending HTTP requests to {0}:", query);
while (Infinite ? true : index <= Requests)

while (Infinite || index <= Requests)
{
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var response = HttpRequest(request);

if (response != null)
DisplayResponse(response, query);
}
catch (WebException e)
{
Error(e.Message + (Timestamp ? " @ " + DateTime.Now.ToString("HH:mm:ss") : ""));
}
catch (Exception e)
{
Error(e.GetType().ToString() + ":" + e.Message + (Timestamp ? " @ " + DateTime.Now.ToString("HH:mm:ss") : ""));
}

index++;
Thread.Sleep(Interval);
}
}

static void DisplayResponse(HttpWebResponse response, string address)
private static HttpWebResponse HttpRequest(HttpWebRequest req)
{
try
{
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
return response;
}
catch (WebException e)
{
Error(e.Message + (Timestamp ? " @ " + DateTime.Now.ToString("HH:mm:ss") : ""));
}
catch (Exception e)
{
Error(e.GetType() + ":" + e.Message + (Timestamp ? " @ " + DateTime.Now.ToString("HH:mm:ss") : ""));
}

return null;
}

private static void DisplayResponse(HttpWebResponse response, string address)
{
// !!HACK ALERT!!

Expand Down Expand Up @@ -244,7 +261,7 @@ static void DisplayResponse(HttpWebResponse response, string address)
Console.WriteLine();
}

static string LookupAddress(string address)
private static string LookupAddress(string address)
{
IPAddress ipAddr = null;

Expand Down Expand Up @@ -274,15 +291,15 @@ static string LookupAddress(string address)
return ipAddr.ToString();
}

static void Error(string msg)
private static void Error(string msg)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(msg);
ResetConsoleColors();
}

static void ResetConsoleColors()
private static void ResetConsoleColors()
{
Console.BackgroundColor = DefaultBackgroundColor;
Console.ForegroundColor = DefaultForegroundColor;
Expand Down