Tag Archives: validate url
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.
VB.NET || How To Check If A String Is A Valid HTTP URL Using VB.NET
The following is a module with functions which demonstrates how to check whether a string is a valid HTTP URL using VB.NET.
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 |
' Is Valid URL ' Declare urls Dim 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 For Each url In urls Debug.Print($"URL: {url}, Is Valid: {Utils.Http.IsValidURL(url)}") Next ' 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 37 38 39 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 18, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Namespace Http Public Module modHttp ''' <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 Function IsValidURL(url As String) As Boolean Dim period = url.IndexOf(".") If period > -1 AndAlso Not url.Contains("@") Then ' Check if there are remnants where the url scheme should be. ' Dont modify string if so Dim colon = url.IndexOf(":") Dim slash = url.IndexOf("/") If (colon = -1 OrElse period < colon) AndAlso (slash = -1 OrElse period < slash) Then url = $"http://{url}" End If End If Dim uriResult As System.Uri = Nothing Dim result = System.Uri.TryCreate(url, System.UriKind.Absolute, uriResult) _ AndAlso (uriResult.Scheme = System.Uri.UriSchemeHttp OrElse uriResult.Scheme = System.Uri.UriSchemeHttps) Return result End Function End Module End Namespace End Namespace ' 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 56 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 18, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Declare urls Dim 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 For Each url In urls Display($"URL: {url}, Is Valid: {Utils.Http.IsValidURL(url)}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' 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.