有Uri.IsWellFormedUriString
和Uri.TryCreate
方法,但它们似乎对于文件路径等返回true
。
为了进行输入验证,我如何检查一个字符串是否是有效的(不一定是活动的)HTTP URL?
有Uri.IsWellFormedUriString
和Uri.TryCreate
方法,但它们似乎对于文件路径等返回true
。
为了进行输入验证,我如何检查一个字符串是否是有效的(不一定是活动的)HTTP URL?
尝试使用以下方法验证HTTP网址(uriName
是您想要测试的URI):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& uriResult.Scheme == Uri.UriSchemeHttp;
或者,如果你想接受HTTP和HTTPS URL作为有效的(根据J0e3gan的评论):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps;
。 - J0e3gan&& uriResult != null
! - Ian Grainger这种方法在 http 和 https 中都能很好地工作。只需要一行代码 :)
if (Uri.IsWellFormedUriString("https://www.google.com", UriKind.Absolute))
MSDN: IsWellFormedUriString函数
file://
或ldap://
),此功能将返回true。此解决方案应与对方案的检查相结合,例如if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) ...
。 - Squigglejavascript:alert("xss")
的 URL 返回 true
。 - Alex from Jitbit试一下:
bool IsValidURL(string URL)
{
string Pattern = @"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$";
Regex Rgx = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return Rgx.IsMatch(URL);
}
它将接受以下类型的URL:
public static bool CheckURLValid(this string source)
{
Uri uriResult;
return Uri.TryCreate(source, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
}
用法:
string url = "htts://adasd.xc.";
if(url.CheckUrlValid())
{
//valid process
}
更新:(单行代码)感谢@GoClimbColorado
public static bool CheckURLValid(this string source) => Uri.TryCreate(source, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttps;
用法:
string url = "htts://adasd.xc.";
if(url.CheckUrlValid())
{
//valid process
}
Uri.TryCreate(source, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttps
。 - GoClimbColoradofile://
,ftp://
),要么拒绝不以http://
或https://
开头的可读的URL(例如www.google.com
)这在处理用户输入时并不好。
我的做法如下:
public static bool ValidHttpURL(string s, out Uri resultURI)
{
if (!Regex.IsMatch(s, @"^https?:\/\/", RegexOptions.IgnoreCase))
s = "http://" + s;
if (Uri.TryCreate(s, UriKind.Absolute, out resultURI))
return (resultURI.Scheme == Uri.UriSchemeHttp ||
resultURI.Scheme == Uri.UriSchemeHttps);
return false;
}
用法:
string[] inputs = new[] {
"https://www.google.com",
"http://www.google.com",
"www.google.com",
"google.com",
"javascript:alert('Hack me!')"
};
foreach (string s in inputs)
{
Uri uriResult;
bool result = ValidHttpURL(s, out uriResult);
Console.WriteLine(result + "\t" + uriResult?.AbsoluteUri);
}
输出:
True https://www.google.com/
True http://www.google.com/
True http://www.google.com/
True http://google.com/
False
http://mooooooooo
实际上是一个有效的 Uri。因此,在插入“http://”之后,您无法检查 Uri.IsWellFormedUriString
,如果您在之前检查它,则任何没有 Scheme
的内容都将被拒绝。也许可以做的是我们检查s.Contains('.')
。 - 41686d6564 stands w. Palestinehttp://
之前使用 IsWellFormedUriString
,那么诸如 google.com
这样的东西就会被拒绝,而如果在添加 http://
之后使用它,则仍将对 http://mooooooooo
返回 true。这就是为什么我建议检查字符串是否包含 .
。 - 41686d6564 stands w. Palestinehttp://google.com
这样的 URL 是被接受的。 - Wouter VanherckUri.TryCreate
后,您可以检查 Uri.Scheme
来确定它是否为 HTTP(s)。
Uri.TryCreate
,如原帖所述,但还要检查结果以确保其Scheme是http或https之一。bool passed =
Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp
|| uriResult.Scheme == Uri.UriSchemeHttps);
问题: 有效的URL应包含以下所有“前缀”:https、http、www。
解决方案:
public static bool IsValidUrl(string webSiteUrl)
{
if (webSiteUrl.StartsWith("www."))
{
webSiteUrl = "http://" + webSiteUrl;
}
return Uri.TryCreate(webSiteUrl, UriKind.Absolute, out Uri uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp
|| uriResult.Scheme == Uri.UriSchemeHttps) && uriResult.Host.Replace("www.", "").Split('.').Count() > 1 && uriResult.HostNameType == UriHostNameType.Dns && uriResult.Host.Length > uriResult.Host.LastIndexOf(".") + 1 && 100 >= webSiteUrl.Length;
}
经过单元测试验证
正面单元测试:
[TestCase("http://www.example.com/")]
[TestCase("https://www.example.com")]
[TestCase("http://example.com")]
[TestCase("https://example.com")]
[TestCase("www.example.com")]
public void IsValidUrlTest(string url)
{
bool result = UriHelper.IsValidUrl(url);
Assert.AreEqual(result, true);
}
负面单元测试:
[TestCase("http.www.example.com")]
[TestCase("http:www.example.com")]
[TestCase("http:/www.example.com")]
[TestCase("http://www.example.")]
[TestCase("http://www.example..com")]
[TestCase("https.www.example.com")]
[TestCase("https:www.example.com")]
[TestCase("https:/www.example.com")]
[TestCase("http:/example.com")]
[TestCase("https:/example.com")]
public void IsInvalidUrlTest(string url)
{
bool result = UriHelper.IsValidUrl(url);
Assert.AreEqual(result, false);
}
注意: IsValidUrl方法不应验证任何类似example.com的相对URL路径。
参见:
Uri.IsWellFormedUriString(a.GetAttribute("href"), UriKind.Absolute)
Uri uri = null;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)
return false;
else
return true;
url
是您需要测试的字符串。