Click to See Complete Forum and Search --> : [RESOLVED] [2.0] how to validate uri?
tr333
Jan 7th, 2007, 03:03 AM
how do i check if a uri is valid and contains "http://" at the start of it?
eg: "www.google.com" is invalid but "http://www.google.com" is valid.
is there some class method in .net to check this or do i need to use regex?
(i have looked at the online msdn, but might have missed something...)
Harsh Gupta
Jan 7th, 2007, 04:20 AM
Use either string.StartsWith() or string.SubString()
string s = "http://w3c.org";
//method 1
if(s.StartsWith("http://"))
MessageBox.Show("URI has http://");
else
MessageBox.Show("URI don't have http://");
//method 2
s = "http//www.google.org";
if(s.Substring(0,7)=="http://")
MessageBox.Show("URI has http://");
else
MessageBox.Show("URI don't have http://");
tr333
Jan 7th, 2007, 04:23 PM
great! thanks for the help :thumb:
penagate
Jan 7th, 2007, 04:34 PM
Also have a look in the System.Uri namespace.
tr333
Jan 7th, 2007, 10:48 PM
Also have a look in the System.Uri namespace.
Thanks for mentioning that again...
Trying to create a new instance of System.Uri without the 'http://' in the address results in an UriFormatException :thumb:
EDIT: System.Uri is a class, not a namespace :)
penagate
Jan 8th, 2007, 12:13 AM
Eh... yes. My mistake.
tr333
Jan 8th, 2007, 04:44 PM
for anyone else that might stumble upon this thread, here is some sample code of my solution to the problem:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(isValidUri(@"www.google.com").ToString());
Console.WriteLine(isValidUri(@"http://www.google.com").ToString());
Console.ReadLine();
}
public bool isValidUri(string uri)
{
try
{
Uri address = new Uri(uri);
return true;
}
catch (UriFormatException ex)
{
return false;
}
}
}
axion_sa
Jan 9th, 2007, 11:40 AM
A regular expression would save you the trouble of excessive overhead involved in exceptions:
http://regexlib.com/Search.aspx?k=url
http://regexlib.com/REDetails.aspx?regexp_id=1363
// RegEx is found at System.Text.RegularExpressions.
/// <summary>
/// <para>Tests if the provided URL is valid.</para>
/// <para>Supports address, IP address, with or without the protocol, optional port numbers, directory path,
/// file name (with extension), and parameter pairs.</para>
/// </summary>
/// <param name="url">The URL to test.</param>
/// <returns>True if the URL is valid, else false.</returns>
private bool IsValidUrl(string url)
{
const string URL_REGEX = @"(?#WebOrIP)((?#protocol)((http|https):\/\/)?(?#subDomain)(([a-zA-Z0-9]+\.(?#domain)[a-zA-Z0-9\-]+(?#TLD)(\.[a-zA-Z]+){1,2})|(?#IPAddress)((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])))+(?#Port)(:[1-9][0-9]*)?)+(?#Path)((\/((?#dirOrFileName)[a-zA-Z0-9_\-\%\~\+]+)?)*)?(?#extension)(\.([a-zA-Z0-9_]+))?(?#parameters)(\?([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?(?#additionalParameters)(\&([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?)*)?";
Regex urlTest = new Regex(URL_REGEX, RegexOptions.IgnoreCase | RegexOptions.Compiled);
return urlTest.IsMatch(url);
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.