解析域/主机名的字符串 [英] Parsing string for Domain / hostName
问题描述
外出客户可以从域名进入网站.他们还可以从他们的联系人输入邮件地址.
Out customers can enter websites from domain names. They also can enter mailadresses from their contacts.
知道我们需要找到哪些网站的客户可以将其域与邮件地址的域相关联.
Know we need to find customers which websited whoose domain can be associated to the domains of the mailadresses.
所以我的想法是从 webadress 和 url 中提取主机并比较它们
So my idea is to extract the host from the webadress and from the url and compare them
那么从 url 获取主机名的最可靠算法是什么?
So what's the most reliable algorithm to get the hostname from a url?
例如一个主机可以是:
foo.com
www.foo.com
http://foo.com
https://foo.com
https://www.foo.com
结果应该总是 foo.com
The result should always be foo.com
推荐答案
与其依赖不可靠的正则表达式,不如使用 System.Uri
为您进行解析.使用这样的代码:
Rather than relying on unreliable regex use System.Uri
to do the parsing for you. Use a code like this:
string uriStr = "www.foo.com";
if (!uriStr.Contains(Uri.SchemeDelimiter)) {
uriStr = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, uriStr);
}
Uri uri = new Uri(uriStr);
string domain = uri.Host; // will return www.foo.com
现在获取您可以使用的顶级域:
string tld = uri.GetLeftPart( UriPartial.Authority ); // will return foo.com
这篇关于解析域/主机名的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!