A better way to validate URL in C# than try-catch?

52,926

Solution 1

Use Uri.TryCreate to create a new Uri object only if your url string is a valid URL. If the string is not a valid URL, TryCreate returns false.

string myString = "http://someUrl";
Uri myUri;
if (Uri.TryCreate(myString, UriKind.RelativeOrAbsolute, out myUri))
{
    //use the uri here
}

UPDATE

TryCreate or the Uri constructor will happily accept strings that may appear invalid, eg "Host: www.stackoverflow.com","Host:%20www.stackoverflow.com" or "chrome:about". In fact, these are perfectly valid URIs that specify a custom scheme instead of "http".

The documentation of the Uri.Scheme property provides more examples like "gopher:" (anyone remember this?), "news", "mailto", "uuid".

An application can register itself as a custom protocol handler as described in MSDN or other SO questions, eg How do I register a custom URL protocol in Windows?

TryCreate doesn't provide a way to restrict itself to specific schemes. The code needs to check the Uri.Scheme property to ensure it contains an acceptable value

UPDATE 2

Passing a weird string like "></script><script>alert(9)</script> will return true and construct a relative Uri object. Calling Uri.IsWellFormedOriginalString will return false though. So you probably need to call IsWellFormedOriginalString if you want to ensure that relative Uris are well formed.

On the other hand, calling TryCreate with UriKind.Absolute will return false in this case.

Interestingly, Uri.IsWellFormedUriString calls TryCreate internally and then returns the value of IsWellFormedOriginalString if a relative Uri was created.

Solution 2

A shortcut would be to use Uri.IsWellFormedUriString:

if (Uri.IsWellFormedUriString(myURL, UriKind.RelativeOrAbsolute))
...

Solution 3

Some examples when using Uri to test a valid URL fails

Uri myUri = null;
if (Uri.TryCreate("Host: www.stackoverflow.com", UriKind.Absolute, out myUri))
{
}

  myUri = null;
if (Uri.TryCreate("Accept: application/json, text/javascript, */*; q=0.01", UriKind.Absolute, out myUri))
{
}

  myUri = null;
if (Uri.TryCreate("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0", UriKind.Absolute, out myUri))
{
}

  myUri = null;
if (Uri.TryCreate("DNT: 1", UriKind.Absolute, out myUri))
{
}

I Was surprised to have all this nonsense appear in my listview after validating with the above. But it all passes the validation test.

Now I add the following after the above validation

url = url.ToLower();
if (url.StartsWith("http://") || url.StartsWith("https://")) return true;

Solution 4

Hi you validate https http,ftp,sftp,ftps,any thing starting with www.

string regular = @"^(ht|f|sf)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$";
string regular123 = @"^(www.)[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$";

string myString = textBox1.Text.Trim();
if (Regex.IsMatch(myString, regular))
{
    MessageBox.Show("It is valide url  " + myString);
}
else if (Regex.IsMatch(myString, regular123))
{
    MessageBox.Show("Valide url with www. " + myString);
}
else 
{
    MessageBox.Show("InValide URL  " + myString);
}

Solution 5

Or this source code good image valid optimization:

 public static string ValidateImage(string absoluteUrl,string defaultUrl)
        { 
           Uri myUri=null; 
           if (Uri.TryCreate(absoluteUrl, UriKind.Absolute, out myUri))
            {
                using (WebClient client = new WebClient())
                {
                    try
                    {
                        using (Stream stream = client.OpenRead(myUri))
                        {
                            Image image = Image.FromStream(stream);
                            return (image != null) ? absoluteUrl : defaultUrl;
                        }
                    }
                    catch (ArgumentException)
                    {
                        return defaultUrl;
                    }
                    catch (WebException)
                    {
                        return defaultUrl;
                    }
                }
            }
            else
            {
                return defaultUrl;
            }
        }

Sou and demo asp.net mvc source image created:

<img src="@ValidateImage("http://example.com/demo.jpg","nophoto.png")"/>
Share:
52,926
Benny Skogberg
Author by

Benny Skogberg

Professional Solutions Architect BSc in Computer Science, major in Information Architecture MCSE: SharePoint MCSA: Office 365 | Linked In | Twitter |

Updated on July 05, 2022

Comments

  • Benny Skogberg
    Benny Skogberg almost 2 years

    I'm building an application to retrieve an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the application.

    (1) Is this the best way to verify URL and handle wrong input - or should I use Regex (or some other method) instead?

    (2) Why does the application try to find images locally if I don't specify http:// in the textBox?

    private void btnGetImage_Click(object sender, EventArgs e)
    {
        String url = tbxImageURL.Text;
        byte[] imageData = new byte[1];
    
        using (WebClient client = new WebClient())
        {
            try
            {
                imageData = client.DownloadData(url);
                using (MemoryStream ms = new MemoryStream(imageData))
                {
                    try
                    {
                        Image image = Image.FromStream(ms);
                        pbxUrlImage.Image = image;
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show("Specified image URL had no match", 
                            "Image Not Found", MessageBoxButtons.OK, 
                            MessageBoxIcon.Error);
                    }
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Image URL can not be an empty string", 
                    "Empty Field", MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
            }
            catch (WebException)
            {
                MessageBox.Show("Image URL is invalid.\nStart with http:// " +
                    "and end with\na proper image extension", "Not a valid URL",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        } // end of outer using statement
    } // end of btnGetImage_Click
    

    EDIT: I tried the suggested solution by Panagiotis Kanavos (thank you for your effort!), but it only gets caught in the if-else statement if the user enters http:// and nothing more. Changing to UriKind.Absolute catches empty strings as well! Getting closer :) The code as of now:

    private void btnGetImage_Click(object sender, EventArgs e)
    {
        String url = tbxImageURL.Text;
        byte[] imageData = new byte[1];
        Uri myUri;
    
        // changed to UriKind.Absolute to catch empty string
        if (Uri.TryCreate(url, UriKind.Absolute, out myUri))
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    imageData = client.DownloadData(myUri);
                    using (MemoryStream ms = new MemoryStream(imageData))
                    {
                        imageData = client.DownloadData(myUri);
                        Image image = Image.FromStream(ms);
                        pbxUrlImage.Image = image;
                    }
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Specified image URL had no match",
                        "Image Not Found", MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
                }
                catch (WebException)
                {
                    MessageBox.Show("Image URL is invalid.\nStart with http:// " +
                        "and end with\na proper image extension", 
                        "Not a valid URL",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        else
        {
            MessageBox.Show("The Image Uri is invalid.\nStart with http:// " +
                "and end with\na proper image extension", "Uri was not created",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    

    I must be doing something wrong here. :(

  • Benny Skogberg
    Benny Skogberg almost 14 years
    Thanx for your quick answer! BR
  • Panagiotis Kanavos
    Panagiotis Kanavos almost 12 years
    That's better if you just want to check the url. TryCreate will also create a Uri to use in the same step.
  • Chris Rogers
    Chris Rogers over 11 years
    This is the best answer to the question that was actually asked. Well done.
  • Martin
    Martin over 11 years
    This fails if pass in a header string such as "Host: www.stackoverflow.com". By fails I mean, fails to detect it as an invalid URL
  • Panagiotis Kanavos
    Panagiotis Kanavos over 11 years
    This is no failure. "Host: www.stackoverflow.com" or the equivalent "Host:%20www.stackoverflow.com" are valid URIs whose scheme is "host", just as "chrome:about" is a valid URI. This is how custom URL protocols are specified. There are many QAs on how to use custom protocols, eg stackoverflow.com/questions/80650/…
  • xiaoyifang
    xiaoyifang over 10 years
    if the url ="\"></script><script>alert(9)</script>",the result is true?
  • Panagiotis Kanavos
    Panagiotis Kanavos over 10 years
    Actually, IsWellFormedUriString actually calls TryCreate to check if the url is valid but then checks whether the returned uri returns true from uri.IsWellFormedOriginalString().
  • Panagiotis Kanavos
    Panagiotis Kanavos over 10 years
    In that case the result is true but the generated relative uri will return false when you call myUri.IsWellFormedOriginalString()
  • noobprogrammer
    noobprogrammer over 4 years
    @ShivamShrivastava. Great! If i have let's say 10 records, and it contains working as well as non-working url, how can I print only the non-working urls in gridview or datatable?
  • Aisah Hamzah
    Aisah Hamzah over 4 years
    Except that a valid uri can start with any scheme, like ftp or file, or even be a URN. You wouldn't catch those. Besides, MS doesn't claim TryCreate requires valid INPUT, instead it does it best to take nonsense input and still make something useful. Use IsWellFormedOriginalString for RFC level validation. Though it only works with absolute uris.
  • Aisah Hamzah
    Aisah Hamzah over 4 years
    TryCreate attempts to create a valid URI from invalid input. It will escape illegal characters and do other trickery (as shown in your example: the input is a false URI, the output is valid) . I.e. ":/" and "##foo" are invalid, but accepted by this method. Conversely, IsWellFormedOriginalString checks validity for it being an absolute URI and would correctly return false here. Valid relative URIs will return false, try "ç.html". Make them absolute, and they validate correctly. As it stands, there's no way to test for RFC valid relative URIs, unless you first make them absolute.
  • Aisah Hamzah
    Aisah Hamzah over 4 years
    This should not be used in real world code, most invalid URIs will pass, and most valid URIs will fail (like "stackoverflow.com", "mailto:x", "urn:test" and any relative URI).
  • Aisah Hamzah
    Aisah Hamzah over 4 years
    This only tests if valid URL is a valid relative URL, and will still pass many invalid inputs too.
  • Aisah Hamzah
    Aisah Hamzah over 4 years
    This only works for absolute URIs, for relative URIs, this method returns false negatives more often than not. But this is consistent with the docs, which explain it works on absolute URIs only.