177

如何从字符串 URL 获取主机域?

GetDomain 有 1 个输入“URL”,1 个输出“域”

示例 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
4

10 回答 10

314

您可以使用Request对象或Uri对象来获取 url 的主机。

使用Request.Url

string host = Request.Url.Host;

使用Uri

Uri myUri = new Uri("http://www.contoso.com:8080/");   
string host = myUri.Host;  // host is "www.contoso.com"
于 2013-01-08T09:37:51.220 回答
81

试试这样;

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

文件://服务器/文件名.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));

Demo

于 2013-01-08T09:41:01.607 回答
30

使用Uri类并使用Host属性

Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345");
Console.WriteLine(url.Host);
于 2013-01-08T09:39:17.597 回答
21

尝试以下语句

 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
于 2014-07-12T05:59:49.143 回答
11

最好的方法,正确的方法是使用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

于 2015-03-27T16:33:26.897 回答
3
 var url = Regex.Match(url, @"(http:|https:)\/\/(.*?)\/");

输入=“https://stackoverflow.com/questions/”;

输出 = "https://stackoverflow.com/";

于 2020-08-21T19:48:41.920 回答
1

试试这个

Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345"));

它将输出 support.domain.com

或者试试

Uri.GetLeftPart( UriPartial.Authority )
于 2013-01-08T09:37:59.313 回答
0

您应该将您的字符串构造为URI对象,并且权限属性返回您需要的内容。

于 2013-01-08T09:42:59.540 回答
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();
        }
    }
于 2021-08-12T00:04:01.540 回答
-3

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;

    }
于 2015-03-27T16:20:38.443 回答