| 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 4 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 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 1 19 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-1 80 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-2 41 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 299 300 301 302 303 304 305 306 307-3 08 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337-338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368-3 69 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398-399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429-4 30 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459-460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490-4 91 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520-521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
* * Author:sunjoy at CCNU * If you have improved this class please send me a code (CCNUSJY in gmail.com) * * using System; Using System.Data; Using System.Configuration; Using System.Net; Using System.IO; Using System.Text; Using System.Collections.Generic; Using System.Text.RegularExpressions; Using System.Threading; Using System.Web; <summary>///Web page class///</summary> public class webpage {#region Private member of Privacy Uri m_uri;/url Private list& Lt Link> m_links; The link on this page is private string m_title; The title of this page is private string m_html; The HTML code for this page is private string m_outstr; This page can be exported as plain text private bool M_good; Whether this web page can be private int m_pagesize; The size of this page is private static dictionary<string, cookiecontainer> webcookies = new dictionary<string, Cookiecontainer> ();//Store cookies private string m_post for all Web pages; The landing page of this page requires the Post data private string M_loginurl; The landing page for this page #endregion #region Private method///<summary>///This private method analyzes the link information from the HTML code of the Web page///</summary>///<returns> ; List<link></returns> Private List<liNk> Getlinks () {if (m_links). Count = = 0) {regex[] Regex = new regex[2]; regex[0] = new Regex ((? m) <a[^><]+href= ("|")? (?<url> ([^> "' s)]) +) (" | ")? [^>]*> (?<text> (w| W) *?) </", Regexoptions.multiline | Regexoptions.ignorecase); REGEX[1] = new Regex ("<[i]*frame[^><]+src=")? (?<url> ([^> "' s)]) +) (" | ")? [^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase); for (int i = 0; i < 2; i++) {Match match = regex[i]. Match (m_html); while (match. Success) {try {string url = new Uri (M_uri, match. groups["url"]. Value). Absoluteuri; String text = ""; if (i = = 0) Text = new Regex (<[^>]+>) | ( s) | () |&| "", Regexoptions.multiline | Regexoptions.ignorecase). Replace (match. groups["Text"]. Value, ""); Link link = new link (url, text); M_links. ADD (link); The catch (Exception ex) {Console.WriteLine (ex). message); }; Match = match. NextMatch (); }} return m_links; ///<summary>///This private method extracts a certain number of words from a paragraph of HTML text///</summary>///<paRam Name= "InStr" >html code </param>///<param name= "FIRSTN" > Extract number of words from scratch </param>///<param name= " Withlink "> Do you want to link inside the word </param>///<returns> Plain Text </returns> private string Getfirstnchar (string instr, int firstn, bool withlink) {if (M_outstr = = "") {m_outstr = instr. Clone () As String; M_outstr = new Regex (@ (? m) <script[^>]*> w| W) *?</script[^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase). Replace (M_outstr, ""); M_outstr = new Regex (@ (? m) <style[^>]*> w| W) *?</style[^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase). Replace (M_outstr, ""); M_outstr = new Regex (@ (? m) <select[^>]*> w| W) *?</select[^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase). Replace (M_outstr, ""); if (!withlink) m_outstr = new Regex (@ (? m) <a[^>]*> (w| W) *?</a[^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase). Replace (M_outstr, ""); Regex Objreg = new System.Text.RegularExpressions.ReGex ("(<[^>]+?>) |", Regexoptions.multiline | Regexoptions.ignorecase); M_outstr = Objreg.replace (M_outstr, ""); Regex OBJREG2 = new System.Text.RegularExpressions.Regex ("(s) +", Regexoptions.multiline | Regexoptions.ignorecase); M_outstr = Objreg2.replace (M_outstr, "");} Return M_OUTSTR. Length > FIRSTN? M_outstr. Substring (0, FIRSTN): m_outstr; ///<summary>///This private method returns an unsigned integer corresponding to an IP address///</summary>///<param name= "x" >ip address </param>/// ;returns></returns> private UINT GETUINTFROMIP (IPAddress x) {byte[] bt = X.getaddressbytes (); uint i = (UINT) (BT [0] * 256 * 256 * 256); i + + (UINT) (BT[1] * 256 * 256); i + + (UINT) (bt[2] * 256); i + = (UINT) (bt[3]); return i; #endregion #region Public Grammar///<summary>///This public method extracts plain text of a certain number of words in a Web page, including link text///</summary>///<param name= " N "> Word </param>///<returns></returns> public string getcontext (int firstn) {return Getfirstnchar ( M_html, FIRSTN, true); }///<summary> This public method extracts the plain text of a certain number of words in a Web page, excluding link text///</summary>///<param name= "firstn" ></param>///<returns> </returns> public string Getcontextwithoutlink (int firstn) {return Getfirstnchar (m_html, FIRSTN, false);}///< Summary>///This public method extracts a certain number of links from the links in this web page that meet a regular///</summary>///<param name= "pattern" > Regular </ param>///<param name= "Count" > number of links returned </param>///<returns>List<Link></returns> Public list<link> Getspeciallinksbyurl (string pattern,int count) {if (m_links). count==0) getlinks (); list<link> speciallinks = new list<link> (); List<link>. Enumerator i; i = M_links. GetEnumerator (); int cnt = 0; while (I.movenext () && cnt<count) {if (new Regex, Regexoptions.multiline | Regexoptions.ignorecase). Match (I.current.url). Success) {speciallinks.add (i.current); cnt++;}} return speciallinks; ///<summary>///This public method extracts a certain number of links from the links in this web page, and the text of the link satisfies a regular///</summary> <param name= "pattern" > Regular </param>///<param name= "Count" > number of links returned </param>///< Returns>list<link></returns> Public list<link> Getspeciallinksbytext (string pattern,int count) {if (m_links). Count = = 0) getlinks (); list<link> speciallinks = new list<link> (); List<link>. Enumerator i; i = M_links. GetEnumerator (); int cnt = 0; while (I.movenext () && CNT < count) {if (new Regex, Regexoptions.multiline | Regexoptions.ignorecase). Match (I.current.text). Success) {speciallinks.add (i.current); cnt++;}} return speciallinks; ///<summary>///This public method obtains links in a certain IP range in all links///</summary>///<param name= "_ip_start" > Start ip</param& Gt <param name= "_ip_end" > Terminate ip</param>///<returns></returns> public list<link> Getspeciallinksbyip (String _ip_start, String _ip_end) {IPAddress Ip_start = Ipaddress.parse (_ip_start); IPAddress ip_end = Ipaddress.parse (_ip_end); if (M_links. Count = = 0) getlinks (); list<link> speciallinks = new list<link> (); List<link>. Enumerator i; i = M_links. GetEnumerator (); while (I.movenext ()) {ipaddress IP; try {IP = dns.gethostentry (new Uri (I.current.url). Host). ADDRESSLIST[0]; The catch {continue} if (GETUINTFROMIP (IP) >=getuintfromip (ip_start) && getuintfromip (IP) <=GETUINTFROMIP (Ip_end)) {Speciallinks.add (i.current);} return speciallinks; ///<summary>///This public method extracts text in plain text of this web page that satisfies a regular type///</summary>///<param name= "pattern" > Regular </param >///<returns> Return text </returns> public string getspecialwords (string pattern) {if (M_outstr = "") Getcontex T (Int16.maxvalue); Regex regex = new Regex (Pattern, Regexoptions.multiline | Regexoptions.ignorecase); Match Mc=regex. Match (M_OUTSTR); if (MC. Success) return to MC. GROUPS[1]. Value; return string. Empty; #endregion #region Constructor private void Init (string _url) {try {M_uri = new Uri (_url); m_links = new list<link> (); m_html = ""; M_outstr = ""; M_title = ""; M_good = true; if (_url. EndsWith (". rar") | | _url. EndsWith (". Dat") | | _url. EndsWith (". msi")) {M_good = false; return;} HttpWebRequest Rqst = (HttpWebRequest) webrequest.create (M_uri); Rqst. AllowAutoRedirect = true; Rqst. Maximumautomaticredirections = 3; Rqst. useragent = "mozilla/4.0" (compatible; MSIE 5.01; Windows NT 5.0) "; Rqst. KeepAlive = true; Rqst. Timeout = 30000; Lock (Webpage.webcookies) {if (WebPage.webcookies.ContainsKey) (M_uri. Host)) Rqst. Cookiecontainer = Webpage.webcookies[m_uri. Host]; else {cookiecontainer cc = new Cookiecontainer (); Webpage.webcookies[m_uri. Host] = cc; Rqst. Cookiecontainer = CC; } HttpWebResponse RSPs = (httpwebresponse) rqst. GetResponse (); Stream sm = RSPs. GetResponseStream (); if (!rsps. Contenttype.tolower (). StartsWith ("text/") | | RSPs. ContentLength > 1 <<) {RSPs. Close (); M_good = false; Return } Encoding cding = System.Text.Encoding.Default; String Contenttype=rsps. Contenttype.tolower (); int IX= ContentType. IndexOf ("charset="); if (ix!=-1) {try {cding = System.Text.Encoding.GetEncoding (RSPs. Contenttype.substring (ix + "CharSet"). Length + 1)); catch {cding = Encoding.default} m_html = new StreamReader (SM, cding). ReadToEnd (); else {m_html = new StreamReader (SM, cding). ReadToEnd (); Regex regex = new Regex (charset= <cding>[^=]+)? "", regexoptions.ignorecase); String strcding = Regex. Match (m_html). groups["Cding"]. Value; try {cding = encoding.getencoding (strcding);} catch{cding = Encoding.default;} byte[] Bytes=encoding.default.getbytes ( M_html. ToCharArray ()); m_html = cding. GetString (bytes); if (m_html. Split ('? '). Length >) {m_html=encoding.default.getstring (bytes);}} M_pagesize = m_html. Length; M_uri = RSPs. ResponseUri; RSPs. Close (); The catch (Exception ex) {Console.WriteLine (ex). Message+m_uri. ToString ()); M_good = false; } public webpage (string _url) {string uurl = ' "; try {uurl = uri.unescapedatastring (_url); _url = Uurl;} catch {}; Regex re = new Regex ("(? <h>[^x00-xff]+)"); Match MC = Re. Match (_url); if (MC. Success) {String Han = mc. groups["H"]. Value; _url = _url. Replace (Han, System.Web.HttpUtility.UrlEncode (Han, encoding.getencoding ("GB2312")); Init (_url); Public webpage (String _url, String _loginurl, String _post) {string uurl = ""; try {uurl = uri.unescapedatastring (_url); _url = Uurl;} catch {}; Regex re = new regex ("(? <h>[^x00-xff]+)"); Match MC = Re. Match (_url); if (MC. Success) {String Han = mc. groups["H"]. Value; _url = _url. Replace (Han, System.Web.HttpUtility.UrlEncode (Han, encoding.getencoding ("GB2312")); } if (_loginurl. Trim () = = "" | | _post. Trim () = = "" | | WebPage.webcookies.ContainsKey (New Uri (_url). Host)) {Init (_url);} else {#region login string indata = _post; m_post = _post; m_loginurl = _loginurl; byte[] bytes = ENC Oding. Default.getbytes (_post); Cookiecontainer Mycookiecontainer = new Cookiecontainer (); try {//Create a new Cookiecontainer to hold the cookie collection HttpWebRequest myHttpWebRequest = (HtTpwebrequest) WebRequest.Create (_loginurl); Create a new HttpWebRequest Myhttpwebrequest.contenttype = "application/x-www-form-urlencoded"; Myhttpwebrequest.allowautoredirect = false; Myhttpwebrequest.useragent = "mozilla/4.0" (compatible; MSIE 5.01; Windows NT 5.0) "; Myhttpwebrequest.timeout = 60000; Myhttpwebrequest.keepalive = true; myhttpwebrequest.contentlength = bytes. Length; Myhttpwebrequest.method = "POST"; Myhttpwebrequest.cookiecontainer = Mycookiecontainer; Set the HttpWebRequest Cookiecontainer for the Mycookiecontainer Stream that was just established Myrequeststream = Myhttpwebrequest.getrequeststream (); Myrequeststream.write (bytes, 0, bytes. Length); Myrequeststream.close (); HttpWebResponse myhttpwebresponse = (HttpWebResponse) myhttpwebrequest.getresponse (); foreach (Cookie ck in myhttpwebresponse.cookies) {mycookiecontainer.add (CK);} myhttpwebresponse.close (); catch {Init (_url); return} #endregion access the page try {m_uri = new Uri (_url) after landing #region, m_links = new list<link> (); m_html = ""; M_outstr = ""; M_title = ""; M_good = true; if (_url. EndsWith (". rar") | | _url. EndsWith (". Dat") | | _url. EndsWith (". msi")) {M_good = false; return;} HttpWebRequest Rqst = (HttpWebRequest) webrequest.create (M_uri); Rqst. AllowAutoRedirect = true; Rqst. Maximumautomaticredirections = 3; Rqst. useragent = "mozilla/4.0" (compatible; MSIE 5.01; Windows NT 5.0) "; Rqst. KeepAlive = true; Rqst. Timeout = 30000; Rqst. Cookiecontainer = Mycookiecontainer; Lock (webpage.webcookies) {Webpage.webcookies[m_uri. Host] = Mycookiecontainer; } HttpWebResponse RSPs = (httpwebresponse) rqst. GetResponse (); Stream sm = RSPs. GetResponseStream (); if (!rsps. Contenttype.tolower (). StartsWith ("text/") | | RSPs. ContentLength > 1 <<) {RSPs. Close (); M_good = false; Return } Encoding cding = System.Text.Encoding.Default; int IX = RSPs. Contenttype.tolower (). IndexOf ("charset="); if (ix!=-1) {try {cding = System.Text.Encoding.GetEncoding (RSPs. Contenttype.substring (ix + "CharSet"). Length + 1)); catch {cding = Encoding.defaulT } m_html = new StreamReader (SM, cding). ReadToEnd (); M_pagesize = m_html. Length; M_uri = RSPs. ResponseUri; RSPs. Close (); The catch (Exception ex) {Console.WriteLine (ex). Message+m_uri. ToString ()); M_good = false; #endregion}} #endregion #region properties///<summary>///This property to obtain the URL of this Web page, read-only///</summary> public string URL { get {return M_uri. Absoluteuri; }///<summary>///This property to obtain the title of this page, read-only///</summary> public string Title {get {if (M_title = = "") {Regex reg = new Regex (@ (? m) <title[^>]*>?<title> (?: w| W) *?) </title[^>]*> ", Regexoptions.multiline | Regexoptions.ignorecase); Match MC = Reg. Match (m_html); if (MC. Success) m_title= MC. groups["title"]. Value.trim (); return m_title; }///<summary>///This property gets all the link information for this page, read-only///</summary> public list<link> Links {get {if m_links. Count = = 0) getlinks (); return m_links; }///<summary>///This property returns all plain text information for this page, read-only///</summary> public string Context {get {if (M_outstr = = "") GetContext (Int16.maxvalue); return m_outstr; }///<summary>///This property gets the size of this page///</summary> public int PageSize {getting {return m_pagesize;}}///<su Mmary>///This property gets all inbound links to this page///</summary> public list<link> insitelinks {getting {return Getspeciallinksbyurl ("^http://" +m_uri.) Host,int16.maxvalue); }///<summary>///This property indicates whether this page is available///</summary> public bool Isgood {get {return m_good;}}///<summar Y>///This property represents the Web site///</summary> public string Host {get {return M_uri for the Web page. Host; }///<summary>///The post data required for the landing page of this page///</summary> public string Poststr {get {return m_post;}}/// ;summary>///The landing page for this page///</summary> public string Loginurl {get {return m_loginurl;}} #endregion}///<s ummary>///Link class///</summary> public class Link {public string url;//Link web address public string text;//link text public Li NK (String _url, string _text) {url = _url; text = _text;}} |