【问题标题】:Getting the domain from a URL encoded string从 URL 编码的字符串中获取域
【发布时间】:2011-05-15 06:44:27
【问题描述】:

另一个初学者的问题,恐怕......

我想知道是否有人可以告诉我从 ASP.Net 中的 UrlEncoded 字符串获取完全限定域名(例如 www.google.com)的最简单和最有效的方法( C#)。

例如:

 http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dsome%20things%20i%20searched%20for

会给我:

 www.google.com

(或者基本上任何我可以检查域以确保其正确的地方。)

我猜第一步可能是UrlDecode,但由于这是我获取字符串的方式,我想我会提到它,以防它更容易编码。

感谢您的帮助!

【问题讨论】:

    标签: c# asp.net url


    【解决方案1】:
    (new URI(System.Web.HttpServerUtility.UrlDecode(url))).Host
    

    供参考:

    URI - http://msdn.microsoft.com/en-us/library/system.uri.aspx

    HttpServerUtility - http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

    【讨论】:

    • 我时不时地输入 new Url( xxx ) 并惊讶这不是我想要的。
    【解决方案2】:

    相信你可以使用 Request.Url.host 来获取主机名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2023-03-16
      • 2011-07-20
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多