欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-31 21:42:46
在实际开发中,需要用到的数据在url中,因此就需要我们来获取到url中有用的信息,涉及到查询、添加、修改、删除等操作,下面我们就具体来了解一下。 1.简单实例...

在实际开发中,需要用到的数据在url中,因此就需要我们来获取到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模块实例

2、实例:

识别字符串中的url

//string source = "工具集:http://www.gongjuji.net";
string source = @"工具集:
  http://www.gongjuji.net
  爱汉字:http://hanzi.tianma3798.cn"; 
list<string> result = urlanalyze.geturllist(source);
foreach (var item in result)
{
  console.writeline(item);
}
//替换成a标签
string result2 = urlanalyze.replacetoa(source);
console.writeline(result2);</string>

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

属性和部分功能模仿了node.js的url模块

源代码定义:

/// <summary>
/// url地址的格式化和反格式化
/// </summary>
public class urlanalyze
{
  /// <summary>
  /// 协议名称
  /// </summary>
  public string protocol { get; set; }
  /// <summary>
  /// 是否以反斜杠结尾
  /// </summary>
  public bool slashes { get; set; }
  /// <summary>
  /// 验证信息,暂时不使用
  /// </summary>
  public string auth { get; set; }
  /// <summary>
  /// 全小写主机部分,包括端口
  /// </summary>
  public string host
  {
    get
    {
      if (this.port == null)
        return this.hostname;
      return string.format("{0}:{1}", this.hostname, this.port);
    }
  }
  /// <summary>
  /// 端口,为空时http默认是80
  /// </summary>
  public int? port { get; set; }
  /// <summary>
  /// 小写主机部分
  /// </summary>
  public string hostname { get; set; }
  /// <summary>
  /// 页面锚点参数部分 #one#two
  /// </summary>
  public string hash { get; set; }
  /// <summary>
  /// 链接查询参数部分(带问号) ?one=1&two=2
  /// </summary>
  public string search { get; set; }
  /// <summary>
  /// 路径部分
  /// </summary>
  public string pathname { get; set; }
  /// <summary>
  /// 路径+参数部分(没有锚点)
  /// </summary>
  public string path
  {
    get
    {
      if (string.isnullorempty(this.search))
        return this.pathname;
      return pathname + search;
    }
  }
  /// <summary>
  /// 转码后的原链接
  /// </summary>
  public string href { get; set; }
 
  /// <summary>
  /// 参数的key=value 列表
  /// </summary>
  private dictionary<string, string=""> _searchlist = null;
  #region 初始化处理
  /// <summary>
  /// 空初始化
  /// </summary>
  public urlanalyze() { _searchlist = new dictionary<string, string="">(); }
  /// <summary>
  /// 初始化处理
  /// </summary>
  ///<param name="url">指定相对或绝对链接
  public urlanalyze(string url)
  {
    //1.转码操作
    this.href = httputility.urldecode(url);
    initparse(this.href);
    //是否反斜杠结尾
    if (!string.isnullorempty(pathname))
      this.slashes = this.pathname.endswith("/");
    //初始化参数列表
    _searchlist = getsearchlist();
  }
  /// <summary>
  /// 将字符串格式化成对象时初始化处理
  /// </summary>
  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);
    }
  }
  /// <summary>
  /// 字符串url格式化时,路径和参数的初始化处理
  /// </summary>
  ///<param name="path">
  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 参数处理
  /// <summary>
  /// 获取当前参数解析结果字典列表
  /// </summary>
  /// <returns></returns>
  public dictionary<string, string=""> getsearchlist()
  {
    if (_searchlist != null)
      return _searchlist;
    _searchlist = new dictionary<string, string="">();
    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;
  }
  /// <summary>
  /// 获取查询参数的值
  /// </summary>
  ///<param name="key">键
  /// <returns></returns>
  public string getsearchvalue(string key)
  {
    return _searchlist[key];
  }
  /// <summary>
  /// 添加参数key=value,如果值已经存在则修改
  /// </summary>
  ///<param name="key">键
  ///<param name="value">值
  /// <returns></returns>
  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);
    }
  }
  /// <summary>
  /// 删除指定key 的键值对
  /// </summary>
  ///<param name="key">键
  public void remove(string key)
  {
    if (_searchlist.any(q => q.key == key))
      _searchlist.remove(key);
  }
  /// <summary>
  /// 获取锚点列表
  /// </summary>
  /// <returns></returns>
  public list<string> gethashlist()
  {
    list<string> list = new list<string>();
    if (!string.isnullorempty(hash))
    {
      list = hash.split('#').where(q => string.isnullorempty(q) == false)
        .tolist();
    }
    return list;
  }
  #endregion
  /// <summary>
  /// 获取最终url地址,
  /// 对参数值就行urlencode 编码后,有可能和原链接不相同
  /// </summary>
  /// <returns></returns>
  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<string, string=""> 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 静态方法
  /// <summary>
  /// 获取源字符串中所有的链接(可能有重复)
  /// </summary>
  ///<param name="content">源字符串
  /// <returns></returns>
  public static list<string> geturllist(string content)
  {
    list<string> list = new list<string>();
    regex re = new regex(@"(?<url>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;
  }
  /// <summary>
  /// 将字符串中的链接成标签
  /// </summary>
  ///<param name="content">
  /// <returns></returns>
  public static string replacetoa(string content)
  {
    regex re = new regex(@"(?<url>http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
    matchcollection mc = re.matches(content);
    foreach (match m in mc)
    {
      content = content.replace(m.result("${url}"), string.format("</url>{0}", m.result("${url}")));
    }
    return content;
  }
  #endregion
}</url></string></string></string></string,></string></string></string></string,></string,></string,></string,>

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: