Asp.net源码专业站
首页->新闻文章->风讯dotNETCMSv1.0免安装版源码>>Foosun.CMS/Collect/Utility.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:风讯dotNETCMSv1.0免安装版源码
当前文件:文件类型 FooSunCMS/Foosun.CMS/Collect/Utility.cs[13K,2009-6-12 11:42:42]打开代码结构图
普通视图
		            
1//=========================================================== 2//== (c)2007 Foosun Inc. by dotNETCMS 1.0 == 3//== Forum:bbs.foosun.net == 4//== website:www.foosun.net == 5//== Address:NO.109 HuiMin ST.,Chengdu ,China == 6//== TEL:86-28-85098980/66026180 == 7//== qq:655071,MSN:ikoolls@gmail.com == 8//== Code By JiangDong == 9//=========================================================== 10using System; 11using System.Collections; 12using System.Collections.Generic; 13using System.Text; 14using System.Text.RegularExpressions; 15using System.Net; 16using System.IO; 17 18namespace Foosun.CMS.Collect 19{ 20 public class Utility 21 { 22 /// <summary> 23 /// 取得网页的内容 24 /// </summary> 25 /// <param name="sUrl">url地址</param> 26 /// <param name="sEncode">编码名称</param> 27 /// <param name="sDocument">返回的网页内容或者是异常</param> 28 /// <returns>有异常返回false</returns> 29 public static string GetPageContent(Uri Url, string sEncode) 30 { 31 try 32 { 33 34 Encoding encoding = System.Text.Encoding.GetEncoding(sEncode); 35 return GetPageContent(Url, encoding); 36 } 37 catch (WebException ex) 38 { 39 throw ex; 40 } 41 catch (Exception ex) 42 { 43 throw ex; 44 } 45 } 46 /// <summary> 47 /// 取得网页的内容 48 /// </summary> 49 /// <param name="sUrl">url地址</param> 50 /// <param name="encoding">编码方式</param> 51 /// <param name="sDocument">返回的网页内容或者是异常</param> 52 /// <returns>有异常返回false</returns> 53 public static string GetPageContent(Uri Url, Encoding encoding) 54 { 55 WebClient webclient = new WebClient(); 56 try 57 { 58 59 webclient.Encoding = encoding; 60 return webclient.DownloadString(Url); 61 } 62 catch (WebException ex) 63 { 64 throw ex; 65 } 66 catch (Exception ex) 67 { 68 throw ex; 69 } 70 finally 71 { 72 webclient.Dispose(); 73 } 74 } 75 /// <summary> 76 /// 处理URL地址,当BranchUrl为一个全名的URL时则返回本身,否则恰当的衔接到BaseUrl后面 77 /// </summary> 78 /// <param name="BaseUrl">完整的URL</param> 79 /// <param name="BranchUrl">分支URL</param> 80 /// <returns></returns> 81 public static string StickUrl(string BaseUrl, string BranchUrl) 82 { 83 if (Regex.Match(BranchUrl, @"^(http|https|ftp|rtsp|mms)://", RegexOptions.IgnoreCase | RegexOptions.Compiled).Success) 84 { 85 return BranchUrl; 86 } 87 else 88 { 89 BaseUrl = BaseUrl.Replace("\\", "/"); 90 BranchUrl = BranchUrl.Replace("\\", "/"); 91 //2007-09-27 ken暂时修改 92 if (Foosun.Common.Input.GetSubString(BranchUrl, 1).ToString() == "/") 93 { 94 return GetLastUrl(BaseUrl, BranchUrl); 95 } 96 //-------------------------- 97 BranchUrl = BranchUrl.TrimStart('/'); 98 if (BranchUrl.IndexOf("../") != 0) 99 { 100 return UrlPlus(BaseUrl, BranchUrl); 101 } 102 else 103 { 104 if (Regex.Match(BaseUrl, @"/$", RegexOptions.Compiled).Success) 105 { 106 BaseUrl = BaseUrl.TrimEnd('/'); 107 } 108 else if (Regex.Match(BaseUrl, @"/[^\./]+\.[^/]+$", RegexOptions.Compiled).Success) 109 { 110 BaseUrl = Regex.Replace(BaseUrl, @"/[^\./]+\.[^/]+$", "", RegexOptions.Compiled); 111 } 112 while (BranchUrl.IndexOf("../") >= 0) 113 { 114 BranchUrl = Regex.Replace(BranchUrl, @"^\.\./", "", RegexOptions.Compiled); 115 BaseUrl = Regex.Replace(BaseUrl, @"/[^/]*$", "", RegexOptions.Compiled); 116 } 117 return BaseUrl + "/" + BranchUrl; 118 } 119 } 120 } 121 //-------------------------------- 122 private static string GetLastUrl(string BaseUrl, string BranchUrl) 123 { 124 BranchUrl = BranchUrl.TrimStart('/'); 125 string Star_url = ""; 126 string End_Url = BaseUrl; 127 if (BaseUrl.IndexOf("//") > 0) 128 { 129 BaseUrl = BaseUrl.Replace("//", "|"); 130 string[] Url_Arr = BaseUrl.Split('|'); 131 Star_url = Url_Arr[0].ToString(); 132 End_Url = Url_Arr[1].ToString(); 133 } 134 if (End_Url.IndexOf("/") > 0) 135 { 136 string[] End_Arr = End_Url.Split('/'); 137 End_Url = End_Arr[0].ToString(); 138 if (Star_url != string.Empty) 139 { 140 return Star_url + "//" + End_Url + "/" + BranchUrl; 141 } 142 else 143 { 144 return End_Url + "/" + BranchUrl; 145 } 146 } 147 else 148 { 149 if (Star_url != string.Empty) 150 { 151 return Star_url + "//" + End_Url + "/" + BranchUrl; 152 } 153 else 154 { 155 return End_Url + "/" + BranchUrl; 156 } 157 } 158 } 159 160 161 162 private static string UrlPlus(string front, string tail) 163 { 164 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, "(http|https|ftp|rtsp|mms)://.+/$", 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 front + "/" + tail; 179 } 180 else if (Regex.Match(front, @"/[^/\.]+\.[^/]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 181 { 182 return Regex.Replace(front, @"/[^/\.]+\.[^/]+$", "", RegexOptions.IgnoreCase | RegexOptions.Compiled) + "/" + tail; 183 } 184 else 185 { 186 return front + "/" + tail; 187 } 188 } 189 /// <summary> 190 /// 获取一个目标的匹配结果 191 /// </summary> 192 /// <param name="input">要匹配的字符串</param> 193 /// <param name="pattern"></param> 194 /// <param name="find"></param> 195 /// <returns></returns> 196 public static Match GetMatch(string input, string pattern, string find) 197 { 198 string _pattn = Regex.Escape(pattern); 199 _pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 200 _pattn = Regex.Replace(_pattn, @"((\\r\\n)|(\\ ))+", @"\s*", RegexOptions.Compiled); 201 if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 202 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)"); 203 else 204 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)"); 205 Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 206 Match m = r.Match(input); 207 return m; 208 } 209 /// <summary> 210 /// 按严格的匹配方式获取一个目标的匹配结果 211 /// </summary> 212 /// <param name="input"></param> 213 /// <param name="pattern"></param> 214 /// <param name="find"></param> 215 /// <returns></returns> 216 public static Match GetMatchRigid(string input, string pattern, string find) 217 { 218 string _pattn = Regex.Escape(pattern); 219 _pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 220 if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 221 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)"); 222 else 223 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)"); 224 Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 225 Match m = r.Match(input); 226 return m; 227 } 228 /// <summary> 229 /// 匹配超级链接地址 230 /// </summary> 231 /// <param name="input"></param> 232 /// <param name="pattern"></param> 233 /// <param name="find"></param> 234 /// <returns></returns> 235 public static Match GetMatchUrl(string input, string pattern, string find) 236 { 237 string _pattn = Regex.Escape(pattern); 238 _pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?"); 239 if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success) 240 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[^'""\ >]+)"); 241 else 242 _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[^'""\ >]+?)"); 243 Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled); 244 Match m = r.Match(input); 245 return m; 246 } 247 } 248 /* 249 // The RequestState class passes data across async calls. 250 public class RequestState 251 { 252 const int BufferSize = 1024; 253 public StringBuilder RequestData; 254 public byte[] BufferRead; 255 public WebRequest Request; 256 public Stream ResponseStream; 257 // Create Decoder for appropriate enconding type. 258 public Decoder StreamDecode = Encoding.UTF8.GetDecoder(); 259 260 public RequestState() 261 { 262 BufferRead = new byte[BufferSize]; 263 RequestData = new StringBuilder(String.Empty); 264 Request = null; 265 ResponseStream = null; 266 } 267 } 268 269 // ClientGetAsync issues the async request. 270 class ClientGetAsync 271 { 272 public static ManualResetEvent allDone = new ManualResetEvent(false); 273 const int BUFFER_SIZE = 1024; 274 public static void BeginReq(string[] HttpURL) 275 { 276 foreach (string singleurl in HttpURL) 277 { 278 try 279 { 280 Uri httpSite = new Uri(singleurl); 281 HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(httpSite); 282 RequestState rs = new RequestState(); 283 rs.Request = wreq; 284 IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs); 285 allDone.WaitOne(); 286 wreq.EndGetResponse(r); 287 } 288 catch (WebException e) 289 { } 290 catch (Exception e) 291 { } 292 } 293 } 294 private static void RespCallback(IAsyncResult ar) 295 { 296 RequestState rs = (RequestState)ar.AsyncState; 297 WebRequest req = rs.Request; 298 WebResponse resp = req.EndGetResponse(ar); 299 Stream ResponseStream = resp.GetResponseStream(); 300 rs.ResponseStream = ResponseStream; 301 IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, 302 BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs); 303 } 304 305 306 private static void ReadCallBack(IAsyncResult asyncResult) 307 { 308 RequestState rs = (RequestState)asyncResult.AsyncState; 309 Stream responseStream = rs.ResponseStream; 310 int read = responseStream.EndRead(asyncResult); 311 if (read > 0) 312 { 313 Char[] charBuffer = new Char[BUFFER_SIZE]; 314 int len = 315 rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0); 316 String str = new String(charBuffer, 0, len); 317 rs.RequestData.Append( 318 Encoding.ASCII.GetString(rs.BufferRead, 0, read)); 319 IAsyncResult ar = responseStream.BeginRead( 320 rs.BufferRead, 0, BUFFER_SIZE, 321 new AsyncCallback(ReadCallBack), rs); 322 } 323 else 324 { 325 if (rs.RequestData.Length > 0) 326 { 327 string strContent; 328 strContent = rs.RequestData.ToString(); 329 } 330 responseStream.Close(); 331 allDone.Set(); 332 } 333 return; 334 } 335 }*/ 336 337} 338
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:风讯dotNETCMSv1.0免安装版源码
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146