-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlOper.cs
194 lines (177 loc) · 5.98 KB
/
UrlOper.cs
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
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections.Specialized;
namespace DotNet.Utilities
{
/// <summary>
/// URL的操作类
/// </summary>
public class UrlOper
{
static System.Text.Encoding encoding = System.Text.Encoding.UTF8;
#region URL的64位编码
public static string Base64Encrypt(string sourthUrl)
{
string eurl = HttpUtility.UrlEncode(sourthUrl);
eurl = Convert.ToBase64String(encoding.GetBytes(eurl));
return eurl;
}
#endregion
#region URL的64位解码
public static string Base64Decrypt(string eStr)
{
if (!IsBase64(eStr))
{
return eStr;
}
byte[] buffer = Convert.FromBase64String(eStr);
string sourthUrl = encoding.GetString(buffer);
sourthUrl = HttpUtility.UrlDecode(sourthUrl);
return sourthUrl;
}
/// <summary>
/// 是否是Base64字符串
/// </summary>
/// <param name="eStr"></param>
/// <returns></returns>
public static bool IsBase64(string eStr)
{
if ((eStr.Length % 4) != 0)
{
return false;
}
if (!Regex.IsMatch(eStr, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
{
return false;
}
return true;
}
#endregion
/// <summary>
/// 添加URL参数
/// </summary>
public static string AddParam(string url, string paramName, string value)
{
Uri uri = new Uri(url);
if (string.IsNullOrEmpty(uri.Query))
{
string eval = HttpContext.Current.Server.UrlEncode(value);
return String.Concat(url, "?" + paramName + "=" + eval);
}
else
{
string eval = HttpContext.Current.Server.UrlEncode(value);
return String.Concat(url, "&" + paramName + "=" + eval);
}
}
/// <summary>
/// 更新URL参数
/// </summary>
public static string UpdateParam(string url, string paramName, string value)
{
string keyWord = paramName+"=";
int index = url.IndexOf(keyWord)+keyWord.Length;
int index1 = url.IndexOf("&", index);
if (index1 == -1)
{
url = url.Remove(index, url.Length - index);
url = string.Concat(url, value);
return url;
}
url = url.Remove(index,index1 - index);
url = url.Insert(index, value);
return url;
}
#region 分析URL所属的域
public static void GetDomain(string fromUrl, out string domain, out string subDomain)
{
domain = "";
subDomain = "";
try
{
if (fromUrl.IndexOf("的名片") > -1)
{
subDomain = fromUrl;
domain = "名片";
return;
}
UriBuilder builder = new UriBuilder(fromUrl);
fromUrl = builder.ToString();
Uri u = new Uri(fromUrl);
if (u.IsWellFormedOriginalString())
{
if (u.IsFile)
{
subDomain = domain = "客户端本地文件路径";
}
else
{
string Authority = u.Authority;
string[] ss = u.Authority.Split('.');
if (ss.Length == 2)
{
Authority = "www." + Authority;
}
int index = Authority.IndexOf('.', 0);
domain = Authority.Substring(index + 1, Authority.Length - index - 1).Replace("comhttp","com");
subDomain = Authority.Replace("comhttp", "com");
if (ss.Length < 2)
{
domain = "不明路径";
subDomain = "不明路径";
}
}
}
else
{
if (u.IsFile)
{
subDomain = domain = "客户端本地文件路径";
}
else
{
subDomain = domain = "不明路径";
}
}
}
catch
{
subDomain = domain = "不明路径";
}
}
/// <summary>
/// 分析 url 字符串中的参数信息
/// </summary>
/// <param name="url">输入的 URL</param>
/// <param name="baseUrl">输出 URL 的基础部分</param>
/// <param name="nvc">输出分析后得到的 (参数名,参数值) 的集合</param>
public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc)
{
if (url == null)
throw new ArgumentNullException("url");
nvc = new NameValueCollection();
baseUrl = "";
if (url == "")
return;
int questionMarkIndex = url.IndexOf('?');
if (questionMarkIndex == -1)
{
baseUrl = url;
return;
}
baseUrl = url.Substring(0, questionMarkIndex);
if (questionMarkIndex == url.Length - 1)
return;
string ps = url.Substring(questionMarkIndex + 1);
// 开始分析参数对
Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
MatchCollection mc = re.Matches(ps);
foreach (Match m in mc)
{
nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
}
}
#endregion
}
}