C# 从 URL 获取主机域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14211973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get host domain from URL?
提问by 001
how to get host domain from a string URL?
如何从字符串 URL 获取主机域?
GetDomain has 1 input "URL", 1 Output "Domain"
GetDomain 有 1 个输入“URL”,1 个输出“域”
Example1
示例 1
INPUT: http://support.domain.com/default.aspx?id=12345
OUTPUT: support.domain.com
Example2
例2
INPUT: http://www.domain.com/default.aspx?id=12345
OUTPUT: www.domain.com
Example3
例3
INPUT: http://localhost/default.aspx?id=12345
OUTPUT: localhost
采纳答案by Adil
You can use Request
object or Uri
object to get host of url.
您可以使用Request
object 或Uri
object 来获取 url 的主机。
Using Request.Url
string host = Request.Url.Host;
Using Uri
使用Uri
Uri myUri = new Uri("http://www.contoso.com:8080/");
string host = myUri.Host; // host is "www.contoso.com"
回答by soniccool
Try this
尝试这个
Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345"));
It will output support.domain.com
它将输出 support.domain.com
Or try
或者试试
Uri.GetLeftPart( UriPartial.Authority )
回答by Habib
回答by Soner G?nül
Try like this;
像这样尝试;
Uri.GetLeftPart( UriPartial.Authority )
Defines the parts of a URI for the Uri.GetLeftPart method.
定义 Uri.GetLeftPart 方法的 URI 部分。
http://www.contoso.com/index.htm?date=today--> http://www.contoso.com
http://www.contoso.com/index.htm#main--> http://www.contoso.com
nntp://news.contoso.com/[email protected] --> nntp://news.contoso.com
file://server/filename.ext --> file://server
http://www.contoso.com/index.htm?date=today--> http://www.contoso.com
http://www.contoso.com/index.htm#main--> http://www.contoso.com
nntp://news.contoso.com/[email protected] --> nntp://news.contoso.com
文件://服务器/文件名.ext --> 文件://服务器
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Authority));
回答by Can Guney Aksakalli
回答by SiwachGaurav
try following statement
尝试以下语句
Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
string pathQuery = myuri.PathAndQuery;
string hostName = myuri.ToString().Replace(pathQuery , "");
Example1
示例 1
Input : http://localhost:4366/Default.aspx?id=notlogin
Ouput : http://localhost:4366
Example2
例2
Input : http://support.domain.com/default.aspx?id=12345
Output: support.domain.com
回答by Xavius Pupuss
WWW is an alias, so you don't need it if you want a domain. Here is my litllte function to get the real domain from a string
WWW 是一个别名,因此如果您想要一个域,则不需要它。这是我从字符串中获取真实域的 litllte 函数
private string GetDomain(string url)
{
string[] split = url.Split('.');
if (split.Length > 2)
return split[split.Length - 2] + "." + split[split.Length - 1];
else
return url;
}
回答by Guillaume Beauvois
The best way, and the right way to do it is using Uri.Authority
field
最好的方法和正确的方法是使用Uri.Authority
字段
Load and use Uri like so :
像这样加载和使用 Uri:
Uri NewUri;
if (Uri.TryCreate([string with your Url], UriKind.Absolute, out NewUri))
{
Console.Writeline(NewUri.Authority);
}
Input : http://support.domain.com/default.aspx?id=12345
Output : support.domain.com
Input : http://www.domain.com/default.aspx?id=12345
output : www.domain.com
Input : http://localhost/default.aspx?id=12345
Output : localhost
If you want to manipulate Url, using Uri object is the good way to do it. https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx
如果你想操作 Url,使用 Uri 对象是一个很好的方法。 https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx