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

时间:2021-11-16 07:18:11

在实际开发中,需要用到的数据在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

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