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

问题描述 投票:211回答:9

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

如何检查字符串是否是用于输入验证的有效(不一定是活动的)HTTP URL?

c# .net validation url uri
9个回答
383
投票

尝试此验证HTTP URL(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);

78
投票

此方法在http和https中均可正常工作。只需一行:)

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

MSDN:IsWellFormedUriString


22
投票
    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
}

7
投票

这里的所有答案要么允许具有其他方案的URL(例如,file://ftp://),要么拒绝不以http://https://(例如,www.google.com)开头的人类可读URL,这在处理用户输入时不好。

我是这样做的:

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

5
投票

Uri.TryCreate之后,您可以检查Uri.Scheme以查看它是否为HTTP(s)。


2
投票

这将返回bool:

Uri.IsWellFormedUriString(a.GetAttribute("href"), UriKind.Absolute)

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

这里url是你必须测试的字符串。


1
投票

试试看:

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&产物= 2
  • HTTP(S)://www.example.com/page#start
  • HTTP(S)://www.example.com中:8080
  • HTTP(S)://127.0.0.1
  • 127.0.0.1
  • 呜呜呜.example.com
  • example.com

0
投票
bool passed = Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)
© www.soinside.com 2019 - 2024. All rights reserved.