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

时间:2011-09-28 05:48:08

标签: c# .net validation url uri

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

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

9 个答案:

答案 0 :(得分:377)

尝试此操作验证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);

答案 1 :(得分:77)

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

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

MSDN:IsWellFormedUriString

答案 2 :(得分: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
}

答案 3 :(得分:7)

这里的所有答案要么允许使用其他方案的URL(例如file://ftp://),要么拒绝不以http://或{{1}开头的人类可读URL }(例如https://在处理用户输入时效果不好

这是我的操作方式:

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);
}

答案 4 :(得分:5)

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

答案 5 :(得分:1)

这将返回bool:

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

答案 6 :(得分:1)

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

此处url是您必须测试的字符串。

答案 7 :(得分:1)

尝试:

@PersistenceContext
private EntityManager entityManager;

// in code
EntityManagerFactory factory = entityManager.getEntityManagerFactory();

它将接受这样的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

答案 8 :(得分:0)

bool passed = Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)