Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

RE Fixed Issue#11 #15

Merged
merged 12 commits into from
Oct 2, 2020
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ msbuild.err
msbuild.wrn
CheckLinkCLI2/CheckLinkCLI2/links.txt
CheckLinkCLI2/CheckLinkCLI2/urls.txt
CheckLinkCLI2/CheckLinkCLI2/Properties/launchSettings.json
*/.vs/
7 changes: 0 additions & 7 deletions CheckLinkCLI2/CheckLinkCLI2/Properties/launchSettings.json

This file was deleted.

60 changes: 24 additions & 36 deletions CheckLinkCLI2/CheckLinkCLI2/WebLinkChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class WebLinkChecker
public void GetAllEndPointWithUri(string url)
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(2.5);
int? statusCode = null;
try
{
Expand All @@ -33,7 +34,8 @@ public void GetAllEndPointWithUri(string url)
//


Task<HttpResponseMessage> httpResponse = httpClient.GetAsync(url);
//Task<HttpResponseMessage> httpResponse = httpClient.GetAsync(url);
Task<HttpResponseMessage> httpResponse = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, new Uri(url)));
HttpResponseMessage httpResponseMessage = httpResponse.Result;
//Console.WriteLine(httpResponseMessage.ToString());
HttpStatusCode httpStatusCode = httpResponseMessage.StatusCode;
Expand Down Expand Up @@ -75,45 +77,31 @@ public void GetAllEndPointWithUri(string url)
}
Console.ResetColor();
}
catch (Exception)
catch (Exception e)
{
Console.Write("[UKN] ");
Console.Write($"{url} ");
//Console.Write($"[{statusCode}] : ");
Console.WriteLine(": Unknown");
unknownCounter++;
}

}

/// <summary>
/// Only checks for web links that return a valid HTTP status code
/// </summary>
public HttpStatusCode GetHttpStatusCode(string url)
{
// Creating a HttpWebRequest
var request = HttpWebRequest.Create(url);

//Setting the Request method HEAD
request.Method = "HEAD";

//try while we are getting a response

try
{
var response = request.GetResponse() as HttpWebResponse;

if (response != null)
if (e.InnerException.Message == "A task was canceled.")
{
results = response.StatusCode;
response.Close();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[404] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write($"{url} ");
//Console.Write($"[{statusCode}] : ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(": Bad (Timeout)");
Console.ForegroundColor = ConsoleColor.Gray;
badCounter++;
}
else
{
Console.Write("[UKN] ");
Console.Write($"{url} ");
//Console.Write($"[{statusCode}] : ");
Console.WriteLine(": Unknown");
unknownCounter++;
}


}
catch (Exception)
{
return results;
}
return results;

}
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Command-Line Interface (CLI) application to check if a web link is active or bro
- Simply type `.\CheckLinkCLI2.exe` + the absolute path to your .txt or .html file
- The application will then parse through all the links in the file and return the http status codes to the cli

### Version

-Type '.\CheckLinkCLI2.exe' + -v or --version

## How to run the application in Visual Studio 2019/2017

- Click the green 'Code' dropdown button and select 'Download Zip'
Expand Down