Method for checking external links

Source: Internet
Author: User
Private   Void  Check (){  Int Currentnum = Currentnum;  If (Currentnum> = Totalcount ){  Return  ;} Datagridviewrow row = This  . Maid [currentnum]; row. cells [  "  ID " ]. Style. backcolor = Color. Green;  String Url = row. cells [ "  URL  "  ]. Value. tostring ();  String Title = row. cells [ "  Title  "  ]. Value. tostring (); Title = System. Web. httputility. htmlencode (title); WebClient Client =New  WebClient (); client. Encoding = Encoding. utf8; Uri URI = New  Uri (URL); system. Io. Stream stream = Null  ;;  Try  {Stream = Client. openread (URI );}  Catch  (System. net. webexception ex ){ If (EX. Status = Webexceptionstatus. protocolerror) Row. defaultcellstyle. backcolor = Color. Yellow;  If (EX. Status = Webexceptionstatus. connectfailure) Row. defaultcellstyle. backcolor = Color. Yellow;  If (EX. Status = Webexceptionstatus. Timeout) Row. defaultcellstyle. backcolor = Color. Yellow;  If (Stream! = Null  ) Stream. Dispose (); client. Dispose ();  Return  ;}  Using (System. Io. memorystream MS = New  System. Io. memorystream ()){  Int Len = 0  ;  Byte [] Buff = New   Byte [ 512  ];  While (LEN = stream. Read (buff, 0 , 512 )> 0  ) {Ms. Write (buff,  0  , Len );}  String Content = System. Text. encoding. utf8.getstring (Ms. toarray (); match charsetmatch = RegEx. Match (content, " <Meta ([^ <] *) charset = \"? '? ([^ <] *) \"  " , Regexoptions. ignorecase | Regexoptions. multiline );  String Encoding = charsetmatch. Groups [ 2  ]. Value;  If ( String  . Isnullorempty (encoding) {Encoding = Client. responseheaders [httpresponseheader. contentencoding];}  If (!String . Isnullorempty (encoding) & encoding. tolower ()! = "  UTF-8  "  ){  If (Encoding. tolower (). Contains ( "  Gzip  "  )){  Using (System. Io. memorystream memory = New System. Io. memorystream () {Ms. Position = 0l  ;  Using (Gzipstream = New  Gzipstream (MS, compressionmode. Decompress )){  While (LEN = gzipstream. Read (buff, 0 , 512 )> 0 ) {Memory. Write (buff,  0  , Len) ;}} content = System. Text. encoding. utf8.getstring (memory. toarray ());}}  Else  {  Try  {  If (Encoding. tolower (). Contains ( " GBK  "  ) Encoding = "  Gb2312  "  ; Content = System. Text. encoding. getencoding (encoding). getstring (Ms. toarray ());}  Catch  {}} Match = Titleregex. Match (content ); If (! Match. Success) {row. defaultcellstyle. backcolor = Color. fromargb ( 255 , 200 , 200 , 200  );}  Else   If (Content. indexof (title) < 0  ) {Row. defaultcellstyle. backcolor = Color. fromargb ( 255 , 200 , 200 , 200  );}}  If (Stream! = Null  ) Stream. Dispose (); row. cells [  "  ID  " ]. Style. backcolor = color. fromargb ( 255 , 108 , 226 ,108  );} 

Recently, we need to check the work of The promotion staff. If there are more than 500 external links per day, the manual check is really not harmonious. Write a checkProgram, Under backup.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.