从任何网址获取准确的域名 [英] Getting exact domain name from any URL
本文介绍了从任何网址获取准确的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从任何Url中提取确切的域名。
I need to extract the exact domain name from any Url.
例如,
网址: http://www.google.com - >域名:google.com
Url : http://www.google.com --> Domain : google.com
网址: http://www.google.co.uk/path1/ path2 - >域:google.co.uk
Url : http://www.google.co.uk/path1/path2 --> Domain : google.co.uk
如何在c#中实现?是否有完整的TLD列表或该任务的解析器?
How can this is possible in c# ? Is there a complete TLD list or a parser for that task ?
推荐答案
您可以使用 Uri Class 访问URI的所有组件:
You can use the Uri Class to access all components of an URI:
var uri = new Uri("http://www.google.co.uk/path1/path2");
var host = uri.Host;
// host == "www.google.co.uk"
但是,没有内置的方法来删除www.google.co.uk上的子域www。您需要实现自己的逻辑,例如,
However, there is no built-in way to strip the sub-domain "www" off "www.google.co.uk". You need to implement your own logic, e.g.
var parts = host.ToLowerInvariant().Split('.');
if (parts.Length >= 3 &&
parts[parts.Length - 1] == "uk" &&
parts[parts.Length - 2] == "co")
{
var result = parts[parts.Length - 3] + ".co.uk";
// result == "google.co.uk"
}
这篇关于从任何网址获取准确的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文