C#中Uri操作-成都快上网建站

C#中Uri操作

  在现在的项目中,使用到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;
        }

本文名称:C#中Uri操作
URL网址:http://kswjz.com/article/pihgeg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流