zoukankan      html  css  js  c++  java
  • 检查外链的方法

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

  • 相关阅读:
    块数据加密模式
    "jobTracker is not yet running"(hadoop 配置)
    平衡搜索树
    Programming Assignment 3: Collinear Points
    Programming Assignment 2: Randomized Queues and Deques
    Programming Assignment 1: Percolation
    1007. Maximum Subsequence Sum (25)
    Link List
    1081. Rational Sum (20)
    strassen algorithm
  • 原文地址:https://www.cnblogs.com/Linjianyu/p/2548953.html
Copyright © 2011-2022 走看看