Uri.IsWellFormedUriString () strange behavior

I recently worked on a project where I need to check a bunch of urls for validity. The URLs don't necessarily exist, they just need to be believable. For this I intended to use Uri.IsWellFormedUri

from the library (avoiding regex manipulation).

However, I noticed that some of the URLs that are found to be erroneous even though they seem to be correct to me. In particular, the problem seems to be coming from the percent encoded portions of the url. Below is a small program showing an example:

class Program
{
    static void Main(string[] args)
    {
        string uriBase = "http://example.com/share.html?title=";
        string leftEncoded = "ab%C3%BCde";
        string rightEncoded = "ef%20%C7%20ghij";

        Console.WriteLine("Now testing if uri is well formed:");
        Console.WriteLine("Combined URI:    " + Uri.IsWellFormedUriString(uriBase + leftEncoded + rightEncoded, UriKind.RelativeOrAbsolute));
        Console.WriteLine("Only left part:  " + Uri.IsWellFormedUriString(uriBase + leftEncoded, UriKind.RelativeOrAbsolute));
        Console.WriteLine("Only right part: " + Uri.IsWellFormedUriString(uriBase + rightEncoded, UriKind.RelativeOrAbsolute));

        Console.ReadLine();
    }
}

      

This program gives the following output:

Now testing if uri is well formed:
Combined URI:    False
Only left part:  True
Only right part: True

      

Why is the long url not accepted as correct? The "Title" part of this string is just a concatenation of the parts being taken over. As far as I know, this url is working correctly and therefore should be valid.

Any help is greatly appreciated!

+3


source to share





All Articles