成都创新互联网站制作重庆分公司

C#中Uri操作-创新互联

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

创新互联自2013年起,先为襄州等服务建站,襄州等地企业,进行企业商务咨询服务。为襄州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
  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;
        }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站栏目:C#中Uri操作-创新互联
文章位置:http://cxhlcq.com/article/cdihjh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部