[ACCEPTED]-C#, Is there a better way to verify URL formatting than IsWellFormedUriString?-notsupportedexception

Accepted answer
Score: 13

The reason Uri.IsWellFormedUriString("htttp://www.google.com", UriKind.Absolute) returns true is because it is 3 in a form that could be a valid Uri. URI 2 and URL are not the same.

See: What's the difference between a URI and a URL?

In your case, I 1 would check that new Uri("htttp://www.google.com").Scheme was equal to http or https.

Score: 8

Technically, htttp://www.google.com is a properly formatted URL, according 5 the URL specification. The NotSupportedException was thrown because htttp isn't a registered 4 scheme. If it was a poorly-formatted URL, you 3 would have gotten a UriFormatException. If you just care about 2 HTTP(S) URLs, then just check the scheme 1 as well.

Score: 4

@Greg's solution is correct. However you 2 can steel using URI and validate all protocols 1 (scheme) that you want as valid.

public static bool Url(string p_strValue)
    if (Uri.IsWellFormedUriString(p_strValue, UriKind.RelativeOrAbsolute))
        Uri l_strUri = new Uri(p_strValue);
        return (l_strUri.Scheme == Uri.UriSchemeHttp || l_strUri.Scheme == Uri.UriSchemeHttps);
        return false;

More Related questions