Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
Added the possibility to get the status code of web page (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Dec 3, 2021
1 parent da39e3a commit 695e2d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions LeoCorpLibrary.Core/NetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,7 @@ public static Task<bool> IsAvailableTestSiteAsync(string site)
task.Start();
return task;
}


}
}
27 changes: 27 additions & 0 deletions LeoCorpLibrary/NetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,32 @@ public static Task<bool> IsAvailableTestSiteAsync(string site)
task.Start();
return task;
}

/// <summary>
/// Gets the status code of a specified website.
/// </summary>
/// <param name="url">The URL of the website.</param>
/// <returns>An <see cref="int"/> value.</returns>
/// <exception cref="WebException"></exception>
public static int GetWebPageStatusCode(string url)
{
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Create a web request

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Get the response of the request
myHttpWebResponse.Close(); // Close the request

return 200; // The request was successfull with no warnings nor errors, so return code 200 - OK.
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
return (int)((HttpWebResponse)e.Response).StatusCode;
}
}
return 400; // An unknown error has occured.
}
}
}

0 comments on commit 695e2d6

Please sign in to comment.