获取准确的域名从任何网址 [英] Getting exact domain name from any URL

查看:177
本文介绍了获取准确的域名从任何网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从任何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类来访问一个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关www.google.co.uk没有内置的方式。你需要实现自己的逻辑,例如


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆