从URL获取主机域名
本文关键字:域名 主机 获取 URL | 更新日期: 2023-09-27 17:51:02
如何从字符串URL获得主机域?
GetDomain有1个输入"URL",1个输出"Domain"
例二
INPUT: http://support.domain.com/default.aspx?id=12345
OUTPUT: support.domain.com
Example2
INPUT: http://www.domain.com/default.aspx?id=12345
OUTPUT: www.domain.com
青年们
INPUT: http://localhost/default.aspx?id=12345
OUTPUT: localhost
您可以使用Request
对象或Uri
对象来获取url的主机
string host = Request.Url.Host;
Uri使用<<p> strong> Uri myUri = new Uri("http://www.contoso.com:8080/");
string host = myUri.Host; // host is "www.contoso.com"
试试;
Uri.GetLeftPart( UriPartial.Authority )
为URI定义URI的各个部分。GetLeftPart方法。
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
文件://服务器/文件名。Ext -> file://server
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Authority));
Demo
使用Uri类并使用Host属性
Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345");
Console.WriteLine(url.Host);
try以下语句
Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
string pathQuery = myuri.PathAndQuery;
string hostName = myuri.ToString().Replace(pathQuery , "");
例二
Input : http://localhost:4366/Default.aspx?id=notlogin
Ouput : http://localhost:4366
Example2
Input : http://support.domain.com/default.aspx?id=12345
Output: support.domain.com
最好的方法,也是正确的方法是使用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
var url = Regex.Match(url, @"(http:|https:)'/'/(.*?)'/");
INPUT = "https://stackoverflow.com/questions/";
输出="https://stackoverflow.com/";
试试这个
Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345"));
它将输出support.domain.com
或者试试
Uri.GetLeftPart( UriPartial.Authority )
您应该将字符串构造为URI对象,并且Authority属性返回所需的内容。
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();
}
}
这是一个适用于各种url的解决方案。
public string GetDomainFromUrl(string url)
{
url = url.Replace("https://", "").Replace("http://", "").Replace("www.", ""); //Remove the prefix
string[] fragments = url.Split('/');
return fragments[0];
}
只接受域名(www.bla.com ->bla)
no Uri required
static string GetDomainNameOnly(string s)
{
string domainOnly = "";
if (!string.IsNullOrEmpty(s))
{
if (s.Contains("."))
{
string domain = s.Substring(s.LastIndexOf('.', s.LastIndexOf('.') - 1) + 1);
string countryDomain = s.Substring(s.LastIndexOf('.'));
domainOnly = domain.Replace(countryDomain, "");
}
else
domainOnly = s;
}
return domainOnly;
}
WWW是一个别名,所以如果你想要一个域名,你不需要它。这是我从字符串
中获取实定义域的小函数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;
}