如何检查一个字符串是否为有效的HTTP URL?

365

Uri.IsWellFormedUriStringUri.TryCreate方法,但它们似乎对于文件路径等返回true

为了进行输入验证,我如何检查一个字符串是否是有效的(不一定是活动的)HTTP URL?


3
不要使用 regex.IsMatch 来验证 URL。 会消耗大量 CPU。 https://dev59.com/p10Z5IYBdhLWcg3wpRlW - inesmar
11个回答

605

尝试使用以下方法验证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);

7
这句话的意思是:这个应该是 uriResult.Scheme 而不是 uriName.Scheme 吗?我正在使用以 String 为第一个参数的 TryCreate 重载,而不是 Uri。 - user1017882
8
您可能希望在uriResult.Scheme == ...中添加更多条件,具体来说是https。这取决于您需要这个的原因,但这个小改变就足以让它完美地为我工作了。 - Fiarr
14
根据@Fiarr的评论,需要做一个“小改变”来适应HTTPS和HTTP URL,即:bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps; - J0e3gan
5
@KailashP说得对 - 我认为没有额外检查的这段代码我不喜欢:&& uriResult != null - Ian Grainger
16
看起来这种技术在75次测试中失败了22次。https://dotnetfiddle.net/XduN3A - whitneyland
显示剩余14条评论

196

这种方法在 http 和 https 中都能很好地工作。只需要一行代码 :)

if (Uri.IsWellFormedUriString("https://www.google.com", UriKind.Absolute))

MSDN: IsWellFormedUriString函数


31
对于非HTTP URI(即任何其他方案,如file://ldap://),此功能将返回true。此解决方案应与对方案的检查相结合,例如if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) ... - Squiggle
这个符合RFC3986标准吗? - Marcus
3
@Squiggle,这正是我希望检查的“所有东西”,因为我正在制作一个下载器。所以,这个答案对我来说是最好的方法。 - Beyondo
问题在于IsWellFormedUriString将末尾的空格渲染为URL的有效部分。而且它并不认为它们是%20,因为在空格后添加一个有效的符号会导致URL无效: "a" - 有效 "a " - 有效 ?! "a a" - 无效 ?!? - Ivan Caravanio
注意:这将对类似于 javascript:alert("xss") 的 URL 返回 true - Alex from Jitbit

36

试一下:

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:
  • http(s)://www.example.com
  • http(s)://stackoverflow.example.com
  • http(s)://www.example.com/page
  • http(s)://www.example.com/page?id=1&product=2
  • http(s)://www.example.com/page#start
  • http(s)://www.example.com:8080
  • http(s)://127.0.0.1
  • 127.0.0.1
  • www.example.com
  • example.com

1
这里有一个被低估的评论。 - Max Almonte

31
    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
}

1
这似乎无法处理www网址。例如:www.google.com被显示为无效。 - Zauberin Stardreamer
1
今天,out参数可以改进Uri.TryCreate(source, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttps - GoClimbColorado
1
这个无法处理像abc、tassdds等随机字符串的情况。 - lazydeveloper

21
所有这里的答案要么允许其他方案的URL(例如file://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

1
这允许通过像“mooooooooo”这样的单词,但与Uri.IsWellFormedUriString一起使用可能很好。 - Epirocks
@Epirocks 这是一个很好的观点。问题在于,http://mooooooooo 实际上是一个有效的 Uri。因此,在插入“http://”之后,您无法检查 Uri.IsWellFormedUriString,如果您在之前检查它,则任何没有 Scheme 的内容都将被拒绝。也许可以做的是我们检查s.Contains('.') - 41686d6564 stands w. Palestine
moooooo本身看起来不像一个URL,因为它没有协议。我的做法是去掉你的正则表达式匹配调用,并将其与IsWellFormedUriString一起使用。 - Epirocks
@Epirocks 没错!问题在于如果你在添加 http:// 之前使用 IsWellFormedUriString,那么诸如 google.com 这样的东西就会被拒绝,而如果在添加 http:// 之后使用它,则仍将对 http://mooooooooo 返回 true。这就是为什么我建议检查字符串是否包含 . - 41686d6564 stands w. Palestine
无论如何,我不想接受没有http或https的url。因此,我首先使用IsWellFormedUriString函数,然后再使用您的函数而不使用正则表达式。bool bResult = (Uri.IsWellFormedUriString(s, UriKind.Absolute) && ValidHttpURL(s, out uriResult)); - Epirocks
需要注意的是,像 http://google.com 这样的 URL 是被接受的。 - Wouter Vanherck

7
< p>在使用 Uri.TryCreate 后,您可以检查 Uri.Scheme 来确定它是否为 HTTP(s)。


5
作为使用正则表达式的替代方法,这段代码使用了Uri.TryCreate,如原帖所述,但还要检查结果以确保其Scheme是http或https之一。
bool passed =
  Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult)
    && (uriResult.Scheme == Uri.UriSchemeHttp
      || uriResult.Scheme == Uri.UriSchemeHttps);

你的回答出现在质量较低的帖子中。即使你的代码已经很明显了,请提供一些解释。 - Harsha Biyani

3

问题: 有效的URL应包含以下所有“前缀”:https、http、www。

  • URL必须包含http://或https://。
  • URL中只能包含一个 www 实例。
  • URL主机名类型必须为DNS。
  • URL最大长度为100个字符。

解决方案:

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路径。

参见:

我应该使用相对URL还是绝对URL?


3
这将返回布尔值:
Uri.IsWellFormedUriString(a.GetAttribute("href"), UriKind.Absolute)

2
我认为OP特别提到了他不喜欢Uri.IsWellFormedUriString,因为它会将文件路径判断为true。你有解决这个问题的方法吗? - Isantipov

1
Uri uri = null;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)
    return false;
else
    return true;

在编程中,这里的url是您需要测试的字符串。

4
null == url 检查是非常冗余的 - JSON

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接