检查外链的方法

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条以上的外链,人工一条一条的检查实在不和谐。写了一个检查的程序,备份下。

转载于:https://www.cnblogs.com/Linjianyu/archive/2012/06/14/2548953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值