[ACCEPTED]-C#, Is there a better way to verify URL formatting than IsWellFormedUriString?-notsupportedexception
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
.
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.
@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);
}
else
{
return false;
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.