【问题标题】:Extract base URl from a string in c#?从 C# 中的字符串中提取基本 URl?
【发布时间】:2013-03-26 16:05:19
【问题描述】:

我目前正在开发一个使用 .NET 1.1 框架的项目,但我被困在了这一点上。我有一个像“http://www.example.com/mypage/default.aspx”这样的字符串,或者它可能是“http://www.example.edu/mypage/default.aspx”或“http://www.example.eu/mypage/default.aspx”。如何从这种字符串中提取基本 URl。

谢谢

【问题讨论】:

  • 你需要使用正则表达式
  • 为什么有这么多“使用 Uri 类”的变体?如果有人已经回答了,最好建议对他们的答案进行任何改进,而不是创建十几个近乎重复的答案。
  • @EricJ。 - 这就是以太网的工作原理 - 任何人同时发布,而不是发生冲突解决 - 只有几个好的人幸存:)
  • 我相信你可以在Uri Properties页面上找到你想要的东西
  • @AlexeiLevenkov 更像是一个懒惰的垃圾问题,应该得到一个懒惰的垃圾答案。不过我会删除它,因为它太冒犯了你……或者我可以带着笑脸离开

标签: c# .net string


【解决方案1】:

您可以使用URI 类来获取主机名。

var uri = new Uri("http://www.example.com/mypage/default.aspx");    
var host = uri.Host;

编辑您可以使用uri.Schemeuri.Port 来获取.Scheme 例如(http, ftp) 和 .Port 获取端口号,例如 (8080)

string host = uri.Host;
string scheme = uri.Scheme;
int port = uri.Port;

您可以使用Uri.GetLeftPart 获取基本 URL。

GetLeftPart 方法返回一个包含最左边的字符串 URI 字符串的一部分,以 part 指定的部分结尾。

var uri = new Uri("http://www.example.com/mypage/default.aspx");    
var baseUri = uri.GetLeftPart(System.UriPartial.Authority);

以下示例显示了一个 URI 以及使用 Scheme、Authority、Path 或 Query MSDN 调用 GetLeftPart 的结果。

【讨论】:

  • 感谢您的快速帮助。它有帮助。也谢谢大家的回复。
  • @HRK:我不明白提供主机的答案如何满足对“基本 URI”的请求。这不是你问的。
  • +1 to this...@spender(也+1)-因为没有真正严格的定义“基本 URI”是什么很难猜出原始海报需要什么...: )
  • 这给出了www.example.com(不包括任何端口或协议)
【解决方案2】:

简答

myUri.GetLeftPart(System.UriPartial.Authority)

长答案
假设“Base URI”的意思类似于http://www.example.com,你可以像这样得到base uri:

var myUri= new Uri("http://www.example.com/mypage/default.aspx");    
var baseUri = myUri.GetLeftPart(System.UriPartial.Authority)

这给出:http://www.example.com

注意:uri.Host 给出:www.example.com(不包括端口或方案)

【讨论】:

  • 这正是我的一个问题所需要的!
  • 我很欣赏这个实际的答案。
【解决方案3】:
var builder = new UriBuilder("http://www.example.com/mypage/default.aspx");
builder.Path = String.Empty;
var baseUri = builder.Uri;
var baseUrl = baseUri.ToString();
// http://www.example.com/

【讨论】:

    【解决方案4】:

    只需为 Uri 类创建一个扩展。在我看来,从一开始就应该存在的东西。它会让你的生活更轻松。

    public static class UriExtensions
     {                   
        public static Uri AttachParameters(this Uri uri, 
                                           NameValueCollection parameters)
        {
            var stringBuilder = new StringBuilder();
            string str = "?";
            for (int index = 0; index < parameters.Count; ++index)
            {
                stringBuilder.Append(str + 
                    System.Net.WebUtility.UrlEncode(parameters.AllKeys[index]) +
                     "=" + System.Net.WebUtility.UrlEncode(parameters[index]));
                str = "&";
            }
            return new Uri(uri + stringBuilder.ToString());
        }
    
        public static string GetBaseUrl(this Uri uri) {
            string baseUrl = uri.Scheme + "://" + uri.Host;
            return baseUrl;
        }
    
        public static string GetBaseUrl_Path(this Uri uri) {
            string baseUrl = uri.Scheme + "://" + uri.Host + uri.AbsolutePath;
            return baseUrl;
        }
     }
    

    用法:

    //url -  https://example.com/api/rest/example?firstname=Linus&lastname=Trovalds
    Uri myUri = new Uri("https://example.com/api/rest/example").AttachParameters(
                new NameValueCollection
                {
                    {"firstname","Linus"},
                    {"lastname","Trovalds"}
                }
              );
    // https://example.com
    string baseUrl = myUri.GetBaseUrl();
    // https://example.com/api/rest/example
    string baseUrlandPath = myUri.GetBaseUrl_Path();
    

    【讨论】:

    • 如果 URI 没有使用默认端口,您还需要附加端口,这是一个潜在的错误
    【解决方案5】:

    如果您想将这 3 个都列在一个列表中,您也可以从这里开始 一旦你得到Host Name,你可以做很多选择,如果你想要.com之后的所有东西,请使用AbsolutePath方法

    var uriList = new List<string>()
                {
                    "http://www.mysite.com/mypage/default.aspx",
                    "http://www.mysite.edu/mypage/default.aspx",
                    "http://www.mysite.eu/mypage/default.aspx"
                };
            var holdList = new List<string>();
            foreach (var uriName in uriList)
            {
                Uri uri = new Uri(uriName);
                holdList.Add(uri.Host);
            }
    

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多