解析字符串域/主机名 [英] Parsing string for Domain / hostName
问题描述
输出的客户可以从域名进入网站。他们还可以从他们的联系人输入mailadresses。
Out customers can enter websites from domain names. They also can enter mailadresses from their contacts.
知道我们需要寻找客户而websited whoose域可以关联到mailadresses的领域。
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
做解析为您服务。使用code是这样的:
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屋!