从任何 URL 获取确切的域名
本文关键字:域名 获取 任何 URL | 更新日期: 2023-09-27 17:56:22
我需要从任何URL中提取确切的域名。
例如
网址 : http://www.google.com --> 域 : google.com
网址 : http://www.google.co.uk/path1/path2 --> 域 : google.co.uk
这在 c# 中怎么可能?是否有完整的TLD列表或该任务的解析器?
Uri 类访问 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"中删除。您需要实现自己的逻辑,例如
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"
}
使用:
new Uri("http://www.stackoverflow.com/questions/5984361/c-sharp-getting-exact-domain-name-from-any-url?s=45faab89-43eb-41dc-aa5b-8a93f2eaeb74#new-answer").GetLeftPart(UriPartial.Authority).Replace("/www.", "/").Replace("http://", ""));
输入:
http://www.stackoverflow.com/questions/5984361/c-sharp-getting-exact-domain-name-from-any-url?s=45faab89-43eb-41dc-aa5b-8a93f2eaeb74#new-answer
输出:
stackoverflow.com
也适用于以下内容。
http://www.google.com → google.com
http://www.google.co.uk/path1/path2 → google.co.uk
http://localhost.intranet:88/path1/path2 → 本地主机内网:88
http://www2.google.com → www2.google.com
试试 System.Uri 类。
http://msdn.microsoft.com/en-us/library/system.uri.aspx
new Uri("http://www.google.co.uk/path1/path2").Host
返回"www.google.co.uk"。从那里开始是字符串操作。:/
使用:
var uri =new Uri(Request.RawUrl); // to get the url from request or replace by your own
var domain = uri.GetLeftPart( UriPartial.Authority );
输入:
Url = http://google.com/?search=true&q=how+to+use+google
结果:
domain = google.com
另一个没有依赖关系的变体:
string GetDomainPart(string url)
{
var doubleSlashesIndex = url.IndexOf("://");
var start = doubleSlashesIndex != -1 ? doubleSlashesIndex + "://".Length : 0;
var end = url.IndexOf("/", start);
if (end == -1)
end = url.Length;
string trimmed = url.Substring(start, end - start);
if (trimmed.StartsWith("www."))
trimmed = trimmed.Substring("www.".Length );
return trimmed;
}
例子:
http://www.google.com → google.com
http://www.google.co.uk/path1/path2 → google.co.uk
http://localhost.intranet:88/path1/path2 → localhost.intranet:88
http://www2.google.com → www2.google.com