服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#Url操作类封装、仿Node.Js中的Url模块实例

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

2021-12-08 13:04天马3798 C#

这篇文章主要介绍了C#Url操作类封装、仿Node.Js中的Url模块,实例分析了C#Url操作类封装的技巧,非常具有实用价值,需要的朋友可以参考下。

在实际开发中,需要用到的数据在url中,因此就需要我们来获取到url中有用的信息,涉及到查询、添加、修改、删除等操作,下面我们就具体来了解一下。

1.简单实例

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//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

?
1
2
3
4
5
6
7
8
9
10
11
12
//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模块

源代码定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/// <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

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

延伸 · 阅读

精彩推荐
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16