【问题标题】:Get host domain from URL?从 URL 获取主机域?
【发布时间】:2015-07-14 08:21:49
【问题描述】:

如何从字符串 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

【问题讨论】:

  • 似乎问题出在 URL 中的 host 上,而不是主机的 domain 上,除非我完全误解了“主机域” (而不仅仅是“主机”)。答案与 Uri.Host 类似的事实支持应该更新问题以更好地反映问题中的所需示例并与已接受的答案保持一致。

标签: c# string url httpwebrequest uri


【解决方案1】:

您可以使用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"

【讨论】:

  • 嗨,我想使用 Request.Url,但 Visual Studio 仍然返回无法解析符号“请求”。我不知道出了什么问题。我使用 Visual Studio 2010 和 Net Framework 4.0。谁能解释一下症状?谢谢
  • 您需要在网页/服务中拥有可用的请求对象,但默认情况下不支持该对象。如果没有可用的 Request 对象,可以使用 Uri 类
【解决方案2】:

试试这个

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

它将输出support.domain.com

或者试试

Uri.GetLeftPart( UriPartial.Authority )

【讨论】:

    【解决方案3】:

    使用Uri 类并使用Host 属性

    Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345");
    Console.WriteLine(url.Host);
    

    【讨论】:

      【解决方案4】:

      这样试试;

      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 --> 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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          试试下面的语句

           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
          

          【讨论】:

          • 如果 myuri.PathAndQuery 是“/”则不起作用,它只是将“/”替换为“”
          【解决方案7】:

          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;
          
              }
          

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:
               var url = Regex.Match(url, @"(http:|https:)\/\/(.*?)\/");
              

              INPUT = "https://stackoverflow.com/questions/";

              OUTPUT = "https://stackoverflow.com/";

              【讨论】:

                【解决方案10】:
                    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();
                        }
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-03-26
                  • 2011-06-17
                  • 1970-01-01
                  • 2012-03-03
                  • 2016-08-15
                  相关资源
                  最近更新 更多