温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NETCMSv1.5(Build0509)完整源码版
当前文件路径:NetCMSv15/NetCMS.Content/Collect/Utility.cs

1//====================================================== 2
//== (c)2008 aspxcms inc by NeTCMS v1.0 == 3
//== Forum:bbs.aspxcms.com == 4
//== Website:www.aspxcms.com == 5
//====================================================== 6
using System; 7
using System.Collections; 8
using System.Collections.Generic; 9
using System.Text; 10
using System.Text.RegularExpressions; 11
using System.Net; 12
using System.IO; 13
14
namespace NetCMS.Content.Collect 15
{ 16
public class Utility 17
{ 18
/// <summary> 19
/// 取得网页的内容 20
/// </summary> 21
/// <param name="sUrl">url地址</param> 22
/// <param name="sEncode">编码名称</param> 23
/// <param name="sDocument">返回的网页内容或者是异常</param> 24
/// <returns>有异常返回false</returns> 25
public static string GetPageContent(Uri Url, string sEncode) 26
{ 27
try 28
{ 29
30
Encoding encoding = System.Text.Encoding.GetEncoding(sEncode); 31
return GetPageContent(Url, encoding); 32
} 33
catch (WebException ex) 34
{ 35
throw ex; 36
} 37
catch (Exception ex) 38
{ 39
throw ex; 40
} 41
} 42
/// <summary> 43
/// 取得网页的内容 44
/// </summary> 45
/// <param name="sUrl">url地址</param> 46
/// <param name="encoding">编码方式</param> 47
/// <param name="sDocument">返回的网页内容或者是异常</param> 48
/// <returns>有异常返回false</returns> 49
public static string GetPageContent(Uri Url, Encoding encoding) 50
{ 51
WebClient webclient = new WebClient(); 52
try 53
{ 54
55
webclient.Encoding = encoding; 56
return webclient.DownloadString(Url); 57
} 58
catch (WebException ex) 59
{ 60
throw ex; 61
} 62
catch (Exception ex) 63
{ 64
throw ex; 65
} 66
finally 67
{ 68
webclient.Dispose(); 69
} 70
} 71
/// <summary> 72
/// 处理URL地址,当BranchUrl为一个全名的URL时则返回本身,否则恰当的衔接到BaseUrl后面 73
/// </summary> 74
/// <param name="BaseUrl">完整的URL</param> 75
/// <param name="BranchUrl">分支URL</param> 76
/// <returns></returns> 77
public static string StickUrl(string BaseUrl, string BranchUrl) 78
{ 79
if (Regex.Match(BranchUrl, @"^(http|https|ftp|rtsp|mms)://", RegexOptions.IgnoreCase | RegexOptions.Compiled).Success) 80
{ 81
return BranchUrl; 82
} 83
else 84
{ 85
BaseUrl = BaseUrl.Replace("\\", "/"); 86
BranchUrl = BranchUrl.Replace("\\", "/"); 87
//2007-09-27 ken暂时修改 88
if (NetCMS.Common.Input.GetSubString(BranchUrl, 1).ToString() == "/") 89
{ 90
return GetLastUrl(BaseUrl, BranchUrl); 91
} 92
//-------------------------- 93
BranchUrl = BranchUrl.TrimStart('/'); 94
if (BranchUrl.IndexOf("../") != 0) 95
{ 96
return UrlPlus(BaseUrl, BranchUrl); 97
} 98
else 99
{ 100
if (Regex.Match(BaseUrl, @"/$", RegexOptions.Compiled).Success) 101
{ 102
BaseUrl = BaseUrl.TrimEnd('/'); 103
} 104
else if (Regex.Match(BaseUrl, @"/[^\./]+\.[^/]+$", RegexOptions.Compiled).Success) 105
{ 106
BaseUrl = Regex.Replace(BaseUrl, @"/[^\./]+\.[^/]+$", "", RegexOptions.Compiled); 107
} 108
while (BranchUrl.IndexOf("../") >= 0) 109
{ 110
BranchUrl = Regex.Replace(BranchUrl, @"^\.\./", "", RegexOptions.Compiled); 111
BaseUrl = Regex.Replace(BaseUrl, @"/[^/]*$", "", RegexOptions.Compiled); 112
} 113
return BaseUrl + "/" + BranchUrl; 114
} 115
} 116
} 117
//-------------------------------- 118
private static string GetLastUrl(string BaseUrl, string BranchUrl) 119
{ 120
BranchUrl = BranchUrl.TrimStart('/'); 121
string Star_url = ""; 122
string End_Url = BaseUrl; 123
if (BaseUrl.IndexOf("//") > 0) 124
{ 125
BaseUrl = BaseUrl.Replace("//", "|"); 126
string[] Url_Arr = BaseUrl.Split('|'); 127
Star_url = Url_Arr[0].ToString(); 128
End_Url = Url_Arr[1].ToString(); 129
} 130
if (End_Url.IndexOf("/") > 0) 131
{ 132
string[] End_Arr = End_Url.Split('/'); 133
End_Url = End_Arr[0].ToString(); 134
if (Star_url != string.Empty) 135
{ 136
return Star_url + "//" + End_Url + "/" + BranchUrl; 137
} 138
else 139
{ 140
return End_Url + "/" + BranchUrl; 141
} 142
} 143
else 144
{ 145
if (Star_url != string.Empty) 146
{ 147
return Star_url + "//" + End_Url + "/" + BranchUrl; 148
} 149
else 150
{ 151
return End_Url + "/" + BranchUrl; 152
} 153
} 154
} 155
156
157
158
private static string UrlPlus(string front, string tail) 159
{ 160
if (Regex.Match(front, "(http|https|ftp|rtsp|mms)://[^/]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 161
{ 162
return front + "/" + tail; 163
} 164
else if (Regex.Match(front, "(http|https|ftp|rtsp|mms)://[^/]+/$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 165
{ 166
return front + tail; 167
} 168
else if (Regex.Match(front, "(http|https|ftp|rtsp|mms)://.+/$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 169
{ 170
return front + tail; 171
} 172
else if (Regex.Match(front, @"/[^/\.]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 173
{ 174
return front + "/" + tail; 175
} 176
else if (Regex.Match(front, @"/[^/\.]+\.[^/]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 177
{ 178
return Regex.Replace(front, @"/[^/\.]+\.[^/]+$", "", RegexOptions.IgnoreCase | RegexOptions.Compiled) + "/" + tail; 179
} 180
else 181
{ 182
return front + "/" + tail; 183
} 184
} 185
/// <summary> 186
/// 获取一个目标的匹配结果 187
/// </summary> 188
/// <param name="input">要匹配的字符串</param> 189
/// <param name="pattern"></param> 190
/// <param name="find"></param> 191
/// <returns></returns> 192
public static Match GetMatch(string input, string pattern, string find) 193
{ 194
string _pattn = Regex.Escape(pattern); 195
_pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 196
_pattn = Regex.Replace(_pattn, @"((\\r\\n)|(\\ ))+", @"\s*", RegexOptions.Compiled); 197
if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 198
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)"); 199
else 200
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)"); 201
Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 202
Match m = r.Match(input); 203
return m; 204
} 205
/// <summary> 206
/// 按严格的匹配方式获取一个目标的匹配结果 207
/// </summary> 208
/// <param name="input"></param> 209
/// <param name="pattern"></param> 210
/// <param name="find"></param> 211
/// <returns></returns> 212
public static Match GetMatchRigid(string input, string pattern, string find) 213
{ 214
string _pattn = Regex.Escape(pattern); 215
_pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 216
if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 217
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)"); 218
else 219
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)"); 220
Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 221
Match m = r.Match(input); 222
return m; 223
} 224
/// <summary> 225
/// 匹配超级链接地址 226
/// </summary> 227
/// <param name="input"></param> 228
/// <param name="pattern"></param> 229
/// <param name="find"></param> 230
/// <returns></returns> 231
public static Match GetMatchUrl(string input, string pattern, string find) 232
{ 233
string _pattn = Regex.Escape(pattern); 234
_pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 235
if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 236
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[^'""\ >]+)"); 237
else 238
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[^'""\ >]+?)"); 239
Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 240
Match m = r.Match(input); 241
return m; 242
} 243
} 244
/* 245
// The RequestState class passes data across async calls. 246
public class RequestState 247
{ 248
const int BufferSize = 1024; 249
public StringBuilder RequestData; 250
public byte[] BufferRead; 251
public WebRequest Request; 252
public Stream ResponseStream; 253
// Create Decoder for appropriate enconding type. 254
public Decoder StreamDecode = Encoding.UTF8.GetDecoder(); 255
256
public RequestState() 257
{ 258
BufferRead = new byte[BufferSize]; 259
RequestData = new StringBuilder(String.Empty); 260
Request = null; 261
ResponseStream = null; 262
} 263
} 264




