C# Url操作类封装、仿Node.Js中的Url模块

1.简单实例

目前常用Url操作,查询、添加、修改、删除链接参数,重构生成链接等功能

//string url = "http://www.gongjuji.net:8081";
//string url = "http://www.gongjuji.net/";
//string url = "http://www.gongjuji.net/abc";
// string url = "http://www.gongjuji.net/abc/1234.html";
string url = "http://www.gongjuji.net/abc/1234.html?name=张三&age=1234#one#two";
//string url = "http://www.gongjuji.net/abc/1234.html?name=&age=#one#two";
//string url = "/abc/123.html?name=张三&age=1234#one#two";
// string url = "https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86&form=%E8%8E%B7%E5%8F%96%E5%85%B3%E6%B3%A8%E8%80%85%E5%88%97%E8%A1%A8%E6%8E%A5%E5%8F%A3%20/user/get";
UrlAnalyze _url = new UrlAnalyze(url);
JObject obj = JObject.FromObject(_url);
Console.WriteLine(obj);
//添加或修改参数
_url.AddOrUpdateSearch("page", "2");
_url.AddOrUpdateSearch("name", "李四");
//重新生成链接参数
Console.WriteLine(_url.GetUrl());
Console.WriteLine(_url.GetUrl(true));

C# Url操作类封装、仿Node.Js中的Url模块_第1张图片

2、实例:

识别字符串中的url

//string source = "工具集:http://www.gongjuji.net";
string source = @"工具集:
    http://www.gongjuji.net
    爱汉字:http://hanzi.tianma3798.cn";

List result = UrlAnalyze.GetUrlList(source);
foreach (var item in result)
{
    Console.WriteLine(item);
}

//替换成a标签
string result2 = UrlAnalyze.ReplaceToA(source);
Console.WriteLine(result2);
C# Url操作类封装、仿Node.Js中的Url模块_第2张图片


属性和部分功能模仿了Node.js的url模块,参考:http://blog.csdn.net/u011127019/article/details/52350172

源代码定义:

/// 
/// Url地址的格式化和反格式化
/// 
public class UrlAnalyze
{
    /// 
    /// 协议名称
    /// 
    public string Protocol { get; set; }
    /// 
    /// 是否以反斜杠结尾
    /// 
    public bool Slashes { get; set; }
    /// 
    /// 验证信息,暂时不使用
    /// 
    public string Auth { get; set; }
    /// 
    /// 全小写主机部分,包括端口
    /// 
    public string Host
    {
        get
        {
            if (this.Port == null)
                return this.HostName;
            return string.Format("{0}:{1}", this.HostName, this.Port);
        }
    }
    /// 
    /// 端口,为空时http默认是80
    /// 
    public int? Port { get; set; }
    /// 
    /// 小写主机部分
    /// 
    public string HostName { get; set; }
    /// 
    /// 页面锚点参数部分 #one#two
    /// 
    public string Hash { get; set; }
    /// 
    /// 链接查询参数部分(带问号) ?one=1&two=2
    /// 
    public string Search { get; set; }
    /// 
    /// 路径部分
    /// 
    public string PathName { get; set; }
    /// 
    /// 路径+参数部分(没有锚点)
    /// 
    public string Path
    {
        get
        {
            if (string.IsNullOrEmpty(this.Search))
                return this.PathName;
            return PathName + Search;
        }
    }
    /// 
    /// 转码后的原链接
    /// 
    public string Href { get; set; }

    /// 
    /// 参数的key=value 列表
    /// 
    private Dictionary _SearchList = null;
    #region 初始化处理
    /// 
    /// 空初始化
    /// 
    public UrlAnalyze() { _SearchList = new Dictionary(); }
    /// 
    /// 初始化处理
    /// 
    /// 指定相对或绝对链接
    public UrlAnalyze(string url)
    {
        //1.转码操作
        this.Href = HttpUtility.UrlDecode(url);
        InitParse(this.Href);
        //是否反斜杠结尾
        if (!string.IsNullOrEmpty(PathName))
            this.Slashes = this.PathName.EndsWith("/");
        //初始化参数列表
        _SearchList = GetSearchList();
    }
    /// 
    /// 将字符串格式化成对象时初始化处理
    /// 
    private void InitParse(string url)
    {
        //判断是否是指定协议的绝对路径
        if (url.Contains("://"))
        {
            // Regex reg = new Regex(@"(\w+):\/\/([^/:]+)(:\d*)?([^ ]*)");
            Regex reg = new Regex(@"(\w+):\/\/([^/:]+)(:\d*)?(.*)");
            Match match = reg.Match(url);
            //协议名称
            this.Protocol = match.Result("$1");
            //主机
            this.HostName = match.Result("$2");
            //端口
            string port = match.Result("$3");
            if (string.IsNullOrEmpty(port) == false)
            {
                port = port.Replace(":", "");
                this.Port = Convert.ToInt32(port);
            }
            //路径和查询参数
            string path = match.Result("$4");
            if (string.IsNullOrEmpty(path) == false)
                InitPath(path);
        }
        else
        {
            InitPath(url);
        }
    }
    /// 
    /// 字符串url格式化时,路径和参数的初始化处理
    /// 
    /// 
    private void InitPath(string path)
    {
        Regex reg = new Regex(@"([^#?& ]*)(\??[^#]*)(#?[^?& ]*)");
        Match match = reg.Match(path);
        //路径和查询参数
        this.PathName = match.Result("$1");
        this.Search = match.Result("$2");
        this.Hash = match.Result("$3");
    }
    #endregion

    #region 参数处理
    /// 
    /// 获取当前参数解析结果字典列表
    /// 
    /// 
    public Dictionary GetSearchList()
    {
        if (_SearchList != null)
            return _SearchList;
        _SearchList = new Dictionary();
        if (!string.IsNullOrEmpty(Search))
        {
            Regex reg = new Regex(@"(^|&)?(\w+)=([^&]*)", RegexOptions.Compiled);
            MatchCollection coll = reg.Matches(Search);
            foreach (Match item in coll)
            {
                string key = item.Result("$2").ToLower();
                string value = item.Result("$3");
                _SearchList.Add(key, value);
            }
        }
        return _SearchList;
    }
    /// 
    /// 获取查询参数的值
    /// 
    /// 键
    /// 
    public string GetSearchValue(string key)
    {
        return _SearchList[key];
    }
    /// 
    /// 添加参数key=value,如果值已经存在则修改
    /// 
    /// 键
    /// 值
    /// 
    public void AddOrUpdateSearch(string key, string value, bool Encode = false)
    {
        if (Encode)
            value = HttpUtility.UrlEncode(value);
        //判断指定键值是否存在
        if (_SearchList.ContainsKey(key))
        {
            _SearchList[key] = value;
        }
        else
        {
            _SearchList.Add(key, value);
        }
    }
    /// 
    /// 删除指定key 的键值对
    /// 
    /// 键
    public void Remove(string key)
    {
        if (_SearchList.Any(q => q.Key == key))
            _SearchList.Remove(key);
    }
    /// 
    /// 获取锚点列表
    /// 
    /// 
    public List GetHashList()
    {
        List list = new List();
        if (!string.IsNullOrEmpty(Hash))
        {
            list = Hash.Split('#').Where(q => string.IsNullOrEmpty(q) == false)
                .ToList();
        }
        return list;
    }
    #endregion
    /// 
    /// 获取最终url地址,
    /// 对参数值就行UrlEncode 编码后,有可能和原链接不相同
    /// 
    /// 
    public string GetUrl(bool EncodeValue = false)
    {
        StringBuilder builder = new StringBuilder();
        if (!string.IsNullOrEmpty(Protocol))
        {
            //如果有协议
            builder.Append(Protocol).Append("://");
        }
        //如果有主机标识
        builder.Append(Host);
        //如果有目录和参数
        if (!string.IsNullOrEmpty(PathName))
        {
            string pathname = PathName;
            if (pathname.EndsWith("/"))
                pathname = pathname.Substring(0, pathname.Length - 1);
            builder.Append(pathname);
        }
        //判断是否反斜杠
        if (Slashes)
        {
            builder.Append("/");
        }
        Dictionary searchList = GetSearchList();
        if (searchList != null && searchList.Count > 0)
        {
            builder.Append("?");
            bool isFirst = true;
            foreach (var item in searchList)
            {
                if (isFirst == false)
                {
                    builder.Append("&");
                }
                isFirst = false;
                builder.AppendFormat("{0}={1}", item.Key, EncodeValue ? HttpUtility.UrlEncode(item.Value) : item.Value);
            }
        }
        //锚点
        builder.Append(Hash);
        return builder.ToString();
    }

    #region 静态方法
    /// 
    /// 获取源字符串中所有的链接(可能有重复)
    /// 
    /// 源字符串
    /// 
    public static List GetUrlList(string content)
    {
        List list = new List();
        Regex re = new Regex(@"(?http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
        MatchCollection mc = re.Matches(content);
        foreach (Match m in mc)
        {
            if (m.Success)
            {
                string url = m.Result("${url}");
                list.Add(url);
            }
        }
        return list;
    }
    /// 
    /// 将字符串中的链接成标签
    /// 
    /// 
    /// 
    public static string ReplaceToA(string content)
    {
        Regex re = new Regex(@"(?http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
        MatchCollection mc = re.Matches(content);
        foreach (Match m in mc)
        {
            content = content.Replace(m.Result("${url}"), String.Format("{0}", m.Result("${url}")));
        }
        return content;
    }
    #endregion
}

所属源代码库:https://github.com/tianma3798/Common

你可能感兴趣的:(C#,C#,Url操作,Url操作实例)