如何处理和解析URL地址

4
说我有一个随机URL列表,这些URL完全格式化:
http://www.example1.com, 
http://example2.biz, 
http://www.example3.co.uk

我有一个文本框和一个下拉列表框,用于URL后缀,可以从中获取我的URL值。

因此,用户正在插入一个URL:

标签:www。,文本框[仅插入域名],DDL [.com、.biz、.co.uk、.net、.info]

假设用户输入了“example3”,并选择了后缀“.biz”

因此,我的值为"example3.biz"

我需要一种方法来比较列表中的URL与用户输入

我想只需拆分字符串('。')即可

因此,前缀的选项为:

www.example

或者只是
example

for the sufix

example.com \ biz \net \info ->  (2 parts)
example.co.uk \org.uk (3 parts) 

所以有很多选择
可能是一个具有2个元素的数组的前缀
www.example

并以3个example.co.uk的后缀结尾

这种方式检查起来很复杂,

我选择使用比较技术通过点来分割URL路径是错误的吗?

我只是想向您展示我开始的方式,但当我意识到这太过复杂时就停止了。

这仍然不能涵盖所有选项,已经太复杂了

if (ListArr[0] != "www")
{ // it means no www so

      compare userArr[0] to ListArr[0] // to check domainMatch

      // and for suffix of domain 
      var DDLValueArr = DDLValue.split('.');
      if(DDLValueArr.length > 1) means it is co.uk or org.uk etc'
      {
             compare DDLValueArr[0] to ListArr[1]   
      }
}

else 
compare userArr[0] to ListArr[1] cause the url in the List starts with "www"

什么是比较用户输入和URL列表的好方法?

你的意思是要比较什么?你是想查看用户是否已经在列表中选择了一个已存在的URI吗? - keyboardP
@keyboardP 是的,完全正确。 - Avia Afer
你是否认为www.example.comexample.com是相同的?如果是,那么每次都可以忽略www - keyboardP
抱歉,我忘了它可能是http://sub.domainName....或者https://sub.domainName....等等。 - Avia Afer
1个回答

2
您可以使用UriUriBuilder创建URI,例如:
List<Uri> uris = new List<Uri>(){ 
   new Uri("http://www.example1.com"), 
   new Uri("https://www.example2.biz"), 
   new Uri("http://www.example3.co.uk")
};

string input = "www.example2.biz";
Uri newUri = new UriBuilder(input).Uri;
if (uris.Any(u=> u.Host == newUri.Host))
{
    MessageBox.Show("Already in the list");
}

请注意,如果uri没有指定方案,则UriBuilder将默认为"http:"。

AbsolutePath 就是 TLD 后面的值,对吧?那么这些情况下它不就都应该是 "/" 吗? - Gray
@Gray:你说得对,我已经修改了代码以比较“Host”。如果他想要包括端口,他必须使用“Authoprity”。 - Tim Schmelter
太好了!看起来很棒。如果有这种类的话,我应该去找一下...但是,完整实现测试比其他任何东西都要好! - Avia Afer
只是为了明确起见,Host 只是 "www.example3.co.uk" 和 "www.example1.com"。因此,"subdomain.example.com" 与 "www.example.com" 是不同的。它适用于协议,如 "http/https/ftp/etc"。 - Gray
1
@Gray昨天忘了感谢你在这里的帮助!谢谢伙计。 - Avia Afer

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