• 检查外链的方法


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

  • 相关阅读:
    2020-2021:时间戳
    全链路压测落地和演进之路
    Socket粘包问题的3种解决方案,最后一种最完美!
    MySQL为Null会导致5个问题,个个致命!
    Maven中pom.xml的packaging类型
    mysql 二进制数据查询
    编写 Dockerfile 生成自定义镜像
    Python自动提取生成博客园年度报告
    [C#] 使用 Excel 和 Math.Net 进行曲线拟合和数据预测
    干货!亲子教育的6个阶段,不妨对照看看,你正处在哪一个阶段?
  • 原文地址:https://www.cnblogs.com/Linjianyu/p/2548953.html
Copyright © 2020-2023  润新知