检查外链的方法
private void Check() { int currentNum = CurrentNum; if (currentNum >= totalCount) { return; } DataGridViewRow row = this.dataGridView1.Rows[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 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 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); }
最近工作需要,需要检查推广人员的工作情况,每天500条以上的外链,人工一条一条的检查实在不和谐。写了一个检查的程序,备份下。