如何从字符串URL获取主机域?
GetDomain有1个输入“URL”,1个输出“Domain”
示例1
INPUT: http://support.domain.com/default.aspx?id=12345
OUTPUT: support.domain.com
例2
INPUT: http://www.domain.com/default.aspx?id=12345
OUTPUT: www.domain.com
示例3
INPUT: http://localhost/default.aspx?id=12345
OUTPUT: localhost
答案 0 :(得分:225)
您可以使用Request
对象或Uri
对象来获取网址。
string host = Request.Url.Host;
使用Uri
Uri myUri = new Uri("http://www.contoso.com:8080/");
string host = myUri.Host; // host is "www.contoso.com"
答案 1 :(得分:43)
试试这个;
Uri.GetLeftPart( UriPartial.Authority )
定义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/123456@contoso.com - > NNTP://news.contoso.com
file://server/filename.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));
答案 2 :(得分:27)
Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345");
Console.WriteLine(url.Host);
答案 3 :(得分:10)
尝试以下声明
Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
string pathQuery = myuri.PathAndQuery;
string hostName = myuri.ToString().Replace(pathQuery , "");
示例1
Input : http://localhost:4366/Default.aspx?id=notlogin
Ouput : http://localhost:4366
例2
Input : http://support.domain.com/default.aspx?id=12345
Output: support.domain.com
答案 4 :(得分:4)
最好的方式,正确的方法是使用Uri.Authority
字段
像这样加载和使用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
如果你想操纵Url,使用Uri对象是一个很好的方法。 https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx
答案 5 :(得分:3)
var url = Regex.Match(url, @"(http:|https:)\/\/(.*?)\/");
INPUT =“ https://stackoverflow.com/questions/”;
OUTPUT =“ https://stackoverflow.com/”;
答案 6 :(得分:1)
试试这个
Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345"));
它将输出support.domain.com
或尝试
Uri.GetLeftPart( UriPartial.Authority )
答案 7 :(得分:0)
答案 8 :(得分:0)
public static string DownloadImage(string URL, string MetaIcon,string folder,string name)
{
try
{
WebClient oClient = new WebClient();
string LocalState = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
string storesIcons = Directory.CreateDirectory(LocalState + folder).ToString();
string path = Path.Combine(storesIcons, name + ".png");
//si la imagen no es valida ej "/icon.png"
if (!TextBoxEvent.IsValidURL(MetaIcon))
{
Uri uri = new Uri(URL);
string DownloadImage = "https://" + uri.Host + MetaIcon;
oClient.DownloadFile(new Uri(DownloadImage), path);
}
//si la imagen tiene todo ej https://www.mercadolibre.com/icon.png
else
{
oClient.DownloadFile(new Uri(MetaIcon), path);
}
return path;
}
catch (Exception ex)
{
return ex.ToString();
}
}
答案 9 :(得分:-2)
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;
}