C# || How To Check If A String Is A Valid HTTP URL Using C#
The following is a module with functions which demonstrates how to check whether a string is a valid HTTP URL using C#.
Note: The following function only checks for valid url formatting. It does not determine if the address behind the url is valid for navigation.
1. Is Valid URL
The example below demonstrates the use of ‘Utils.Http.IsValidURL‘ to check whether a string is a valid HTTP URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
// Is Valid URL // Declare urls var urls = new string[] { "https://www.programmingnotes.org", "http://www.programmingnotes.org", "www.programmingnotes.org", "programmingnotes.org", "javascript:alert('programmingnotes.org')", "programmingnotes", "programmingnotes.org/path?queryparam=123", "t:/www.programmingnotes.org", ":programmingnotes.org", "http/programmingnotes.org", "h91ps://programmingnotes.org/", "//programmingnotes.org/", "www@.programmingnotes.org", "protocol://programmingnotes.org/path?queryparam=123", ":programmingnotes.org/", ":programmingnotes.org:80/", "programmingnotes.org:80/", "mailto:test@email.com", "test@email.com", "file:///path/to/file/text.doc", "http://192.168.1.1/", "192.168.1.1" }; // Check if valid foreach (var url in urls) { Console.WriteLine($"URL: {url}, Is Valid: {Utils.Http.IsValidURL(url)}"); } // expected output: /* URL: https://www.programmingnotes.org, Is Valid: True URL: http://www.programmingnotes.org, Is Valid: True URL: www.programmingnotes.org, Is Valid: True URL: programmingnotes.org, Is Valid: True URL: javascript:alert('programmingnotes.org'), Is Valid: False URL: programmingnotes, Is Valid: False URL: programmingnotes.org/path?queryparam=123, Is Valid: True URL: t:/www.programmingnotes.org, Is Valid: False URL: :programmingnotes.org, Is Valid: False URL: http/programmingnotes.org, Is Valid: False URL: h91ps://programmingnotes.org/, Is Valid: False URL: //programmingnotes.org/, Is Valid: False URL: www@.programmingnotes.org, Is Valid: False URL: protocol://programmingnotes.org/path?queryparam=123, Is Valid: False URL: :programmingnotes.org/, Is Valid: False URL: :programmingnotes.org:80/, Is Valid: False URL: programmingnotes.org:80/, Is Valid: True URL: mailto:test@email.com, Is Valid: False URL: test@email.com, Is Valid: False URL: file:///path/to/file/text.doc, Is Valid: False URL: http://192.168.1.1/, Is Valid: True URL: 192.168.1.1, Is Valid: True */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 11, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Http { /// <summary> /// Determines if a url is valid or not /// </summary> /// <param name="url">The url to check</param> /// <returns>True if a url is valid, false otherwise</returns> public static bool IsValidURL(string url) { var period = url.IndexOf("."); if (period > -1 && !url.Contains("@")) { // Check if there are remnants where the url scheme should be. // Dont modify string if so var colon = url.IndexOf(":"); var slash = url.IndexOf("/"); if ((colon == -1 || period < colon) && (slash == -1 || period < slash)) { url = $"http://{url}"; } } System.Uri uriResult = null; var result = System.Uri.TryCreate(url, System.UriKind.Absolute, out uriResult) && (uriResult.Scheme == System.Uri.UriSchemeHttp || uriResult.Scheme == System.Uri.UriSchemeHttps); return result; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 11, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; public class Program { static void Main(string[] args) { try { // Declare urls var urls = new string[] { "https://www.programmingnotes.org", "http://www.programmingnotes.org", "www.programmingnotes.org", "programmingnotes.org", "javascript:alert('programmingnotes.org')", "programmingnotes", "programmingnotes.org/path?queryparam=123", "t:/www.programmingnotes.org", ":programmingnotes.org", "http/programmingnotes.org", "h91ps://programmingnotes.org/", "//programmingnotes.org/", "www@.programmingnotes.org", "protocol://programmingnotes.org/path?queryparam=123", ":programmingnotes.org/", ":programmingnotes.org:80/", "programmingnotes.org:80/", "mailto:test@email.com", "test@email.com", "file:///path/to/file/text.doc", "http://192.168.1.1/", "192.168.1.1" }; // Check if valid foreach (var url in urls) { Display($"URL: {url}, Is Valid: {Utils.Http.IsValidURL(url)}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
According you Regex syntax, even “t:/www.programmingnotes.org” will return true.
Hi, thanks for visiting!
A more restrictive check has been added when processing human readable urls.
Thanks for pointing that out!