3

我正在使用带有此代码的 .net 2010 c# windows 应用程序:检查 Valid Uri or not

代码:

static bool IsValidUrl(string urlString)
{
    Uri uri;
    return Uri.TryCreate(urlString, UriKind.Absolute, out uri)
        && (uri.Scheme == Uri.UriSchemeHttp
         || uri.Scheme == Uri.UriSchemeHttps
         || uri.Scheme == Uri.UriSchemeFtp
         || uri.Scheme == Uri.UriSchemeMailto
         );
}

问题:如果我验证这个http://http://www.Google.com我得到它的有效但是当我尝试使用 IE 时它没有显示任何站点。

有没有办法找出 String 是否是有效的 uri?(不使用正则表达式和互联网访问)

4

3 回答 3

5

这不是一个无效的 URI,甚至不是一个永远不会工作的 URI:您可以在浏览器中使用它,那里有一台名为“http”的本地机器(或者如果您将 Hosts 文件设置为调用一台机器)。

问题是完全正确的 URI http://http://www.Google.com,通常会在表单中使用http://http//www.Google.com,因为我们通常不包括:在主机之后,除非我们包含端口号,因为它无法找到名为的机器“http”。

现在,即使这有时会起作用,当然也不会一直起作用。所以这是一个与 URI 不同的问题http://www.thisdoesnotexistbecauseijustmdeitup.com/

如果您还需要检测这种情况,那么除了连接到 Internet 之外真的别无他法。

如果您需要检测将在全球范围内工作的 URI,而不仅仅是在特定 LAN 上,那么:

static bool IsGloballyUsableWebMailorFtpUrl(string urlString)
{
  Uri uri;
  if(!Uri.TryCreate(urlString, UriKind.Absolute, out uri))
    return false;
  if(uri.Scheme != Uri.UriSchemeHttp
     && uri.Scheme != Uri.UriSchemeHttps
     && uri.Scheme != Uri.UriSchemeFtp
     && uri.Scheme != Uri.UriSchemeMailto)
     return false;
  string host = uri.Host;
  IPAddress ip;
  if(!IPAddress.TryParse(host, out ip))//if we don't have an IP address in the host part.
    return host.Contains('.') && !host.EndsWith(".local", StringComparison.OrdinalIgnoreCase); // Does the domain have at least one period
                                                   // And not the "local" binding used on many
                                                   // Private networks
  var octets = ip.GetAddressBytes();
  if(octets.Length == 4)
    switch(octets[0])//We've an IPv4 IP address, check it's not reserved.
    {
      case 0: case 10: case 127:
        return false;
      case 128: case 191:
        return octets[1] != 0;
      case 169:
        return octets[1] != 254;
      case 172:
        return octets[1] < 16 || octets[1] > 31;
      case 192:
        return octets[1] != 168 && (octets[1] != 0 || octets[2] != 0);
      case 223:
        return octets[1] != 255 && octets[2] != 255;
      default:
        return true;
    }
  else
    {  //We've an IPv6 IP address, check it's not reserved.
      if(IPAddress.HostToNetworkOrder(1) != 1)
        octets = octets.Reverse().ToArray();
      var ipInt = new BigInteger(octets);
      //Not the neatest approach, but serves
      if(ipInt < 0)
        return true;
      if(ipInt < 2)
        return false;
      if(ipInt < 281470681743360)
        return true;
      if(ipInt < 281474976710656)
        return false;
      if(ipInt < BigInteger.Parse("524413980667603649783483181312245760"))
        return true;
      if(ipInt < BigInteger.Parse("524413980667603649783483185607213056"))
        return false;
      if(ipInt < BigInteger.Parse("42540488161975842760550356425300246528"))
        return true;
      if(ipInt < BigInteger.Parse("42540488241204005274814694018844196864"))
        return false;
      if(ipInt < BigInteger.Parse("42540489429626442988779757922003451904"))
        return true;
      if(ipInt < BigInteger.Parse("42540490697277043217009159418706657280"))
        return false;
      if(ipInt < BigInteger.Parse("42540766411282592856903984951653826560"))
        return true;
      if(ipInt < BigInteger.Parse("42540766490510755371168322545197776896"))
        return false;
      if(ipInt < BigInteger.Parse("42545680458834377588178886921629466624"))
        return true;
      if(ipInt < BigInteger.Parse("42550872755692912415807417417958686720"))
        return false;
      if(ipInt < BigInteger.Parse("334965454937798799971759379190646833152"))
        return true;
      if(ipInt < BigInteger.Parse("337623910929368631717566993311207522304"))
        return false;
      if(ipInt < BigInteger.Parse("338288524927261089654018896841347694592"))
        return true;
      if(ipInt < BigInteger.Parse("338620831926207318622244848606417780736"))
        return false;
      if(ipInt < BigInteger.Parse("338953138925153547590470800371487866880"))
        return true;
      if(ipInt < BigInteger.Parse("340282366920938463463374607431768211456"))
        return false;
      return true;
    }
}

编辑:值得考虑是否应该进行此检查,如果它是用于最终将连接到相关 URI 的应用程序,那么您只会通过拒绝连接到他们局域网上的机器来惹恼用户。

于 2013-11-15T10:48:53.903 回答
2

了解给定字符串是否代表有效 url 的最佳方法是执行自定义分析,无需实际测试并记住上面的注释(可能适合给定模式的内容,但不是您认为正确的内容) . 此外,您应该bool用一个string(或一个Uri)能够纠正某些情况(如您提出的示例)的函数替换您的函数。示例代码:

private void Form1_Load(object sender, EventArgs e)
{
    string rightUrl = returnValidUrl("http://http://www.Google.com");
    if (rightUrl != "")
    {
        //It is OK
    }
}

static string returnValidUrl(string urlString)
{
    string outUrl = "";
    Uri curUri = IsValidUrl(urlString);
    if (curUri != null)
    {
        string headingBit = "http://";
        if (curUri.Scheme == Uri.UriSchemeHttps) headingBit = "https://";
        if (curUri.Scheme == Uri.UriSchemeFtp) headingBit = "ftp://";
        if (curUri.Scheme == Uri.UriSchemeMailto) headingBit = "mailto:";

        outUrl = headingBit + urlString.ToLower().Substring(urlString.ToLower().LastIndexOf(headingBit) + headingBit.Length);
    }

    return outUrl;
}

static Uri IsValidUrl(string urlString)
{
    Uri uri = null;
    bool isValid = Uri.TryCreate(urlString, UriKind.Absolute, out uri)
        && (uri.Scheme == Uri.UriSchemeHttp
         || uri.Scheme == Uri.UriSchemeHttps
         || uri.Scheme == Uri.UriSchemeFtp
         || uri.Scheme == Uri.UriSchemeMailto
         );

    if (!isValid) uri = null;

    return uri;
}

可以调用什么:

string rightUrl = returnValidUrl("http://http://www.Google.com");
if (rightUrl != "")
{
    //It is OK
}

您必须扩展此方法以将您需要的所有情况识别为有效/正确。

更新

正如通过评论所建议的那样,并且为了提供 OP 正在寻找的确切功能(它的一个示例;就所提出的解决方案而言,它只是该问题所需的推理方法类型的一个示例),在这里你有bool考虑到发布的示例错误的更正函数:

static bool IsValidUrl2(string urlString)
{
    Uri uri;
    return Uri.TryCreate(urlString, UriKind.Absolute, out uri)
        && ((uri.Scheme == Uri.UriSchemeHttp && numberOfBits(urlString.ToLower(), "http://") == 1)
         || (uri.Scheme == Uri.UriSchemeHttps && numberOfBits(urlString.ToLower(), "https://") == 1)
         || (uri.Scheme == Uri.UriSchemeFtp && numberOfBits(urlString.ToLower(), "ftp://") == 1)
         || (uri.Scheme == Uri.UriSchemeMailto && numberOfBits(urlString.ToLower(), "mailto:") == 1)
         );
}

static int numberOfBits(string inputString, string bitToCheck)
{
    return inputString.ToLower().Split(new string[] { bitToCheck.ToLower() }, StringSplitOptions.None).Length - 1;
}

澄清

完全确定给定 url 是否有效的唯一方法是实际测试它;但是OP说没有我理解为纯字符串分析的联系:这个答案到底是什么。无论如何,正如评论所解释的那样,这篇文章的意图只是展示了一种方式:.NET + 自定义算法(通过理解依靠字符串分析来瞄准整体适用性非常困难);我的提议解释了 OP(重复的“标题部分”)并依赖他的条件解释的具体问题。它根本不能被理解为一种普遍适用、盲目使用的方法;但作为具有示例功能的通用框架(仅仅是概念证明)。

澄清 2

正如下面评论中与 Jon Hanna 的对话所示,还有第三种我不知道的选择:分析未来的 IP 地址(即,数字已经放在一起,但尚未检查 IP 地址的可用性,因此是确定的IP 地址生成未开始);通过查看它,还可以确定给定字符串是有效 URL 地址的可能性(在预期条件下)。无论如何,这也不能被视为 100% 可靠的过程,因为所分析的 IP 地址不是最终的。无论如何,乔恩·汉纳 (Jon Hanna) 比我更能谈论这种替代方案的局限性。

于 2013-11-15T09:53:11.597 回答
1

您可以编写一个自定义函数来检查 http:// 或初始部分是否与您编写的此代码一起重复。

于 2013-11-15T09:41:29.043 回答