C#中Uri操作

发布时间:2020-07-15 14:33:31 作者:彭泽0902
来源:网络 阅读:14399

  在现在的项目中,使用到uri的部分较多。

  1. uri的解析方法:

 public static Uri ReturnUri(this string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                throw new ArgumentNullException(val);
            }
            try
            {
                UriBuilder uri;

                if (val.StartsWith("http://", StringComparison.Ordinal))
                {
                    uri = new UriBuilder(val);
                }
                else if (val.StartsWith("https://", StringComparison.Ordinal))
                {
                    uri = new UriBuilder(val);
                }
                else if (val.Contains(":"))
                {
                    var split = val.Split(':', '/');
                    int port;
                    int.TryParse(split[1], out port);

                    uri = split.Length == 3
                        ? new UriBuilder(Uri.UriSchemeHttp, split[0], port, "/" + split[2])
                        : new UriBuilder(Uri.UriSchemeHttp, split[0], port);
                }
                else
                {
                    uri = new UriBuilder(Uri.UriSchemeHttp, val);
                }

                return uri.Uri;
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message, exception);
            }
        }

2.uri解析的重载:

  public static Uri ReturnUri(this string val, int port, bool ssl = default(bool))
        {
            if (string.IsNullOrEmpty(val))
            {
                throw new ArgumentNullException(val);
            }
            try
            {
                UriBuilder uri;

                if (val.StartsWith("http://", StringComparison.Ordinal))
                {
                    var split = val.Split('/');
                    uri = split.Length >= 4 ? new UriBuilder(Uri.UriSchemeHttp, split[2], port, "/" + split[3]) : new UriBuilder(new Uri($"{val}:{port}"));
                }
                else if (val.StartsWith("https://", StringComparison.Ordinal))
                {
                    var split = val.Split('/');
                    uri = split.Length >= 4
                        ? new UriBuilder(Uri.UriSchemeHttps, split[2], port, "/" + split[3])
                        : new UriBuilder(Uri.UriSchemeHttps, split[2], port);
                }
                else if(ssl)
                {
                    uri = new UriBuilder(Uri.UriSchemeHttps, val, port);
                }
                else
                {
                    uri = new UriBuilder(Uri.UriSchemeHttp, val, port);
                }

                return uri.Uri;
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message, exception);
            }
        }


  public static Uri ReturnUriWithSubDir(this string val, int port, bool ssl, string subDir)
        {
            var uriBuilder = new UriBuilder(val);
            if (ssl)
            {
                uriBuilder.Scheme = Uri.UriSchemeHttps;
            }
            if (!string.IsNullOrEmpty(subDir))
            {
                uriBuilder.Path = subDir;
            }
            uriBuilder.Port = port;

            return uriBuilder.Uri;
        }


推荐阅读:
  1. WPF下使用Uri的案例
  2. nginx location中uri的截取

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c uri

上一篇:如何使用Redis实现秒杀

下一篇:Redis有哪些常用的用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》