using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CheckDomain
{

    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Collections;
    public delegate void CallBack(DataGridViewRow dgrv, DomainStatus ds);
    public delegate void CallBackEx(string dgrv, DomainStatus ds);
    public class ThreadWithData
    {
        public UrlCheck uc = new UrlCheck();
        public CallBack CBack;
        public CallBackEx CBackEx;
        public ArrayList DomainList;
        public DataGridViewRow[] DataGridViewRowDomainList;

        public ThreadWithData(ArrayList tempDomain, CallBack tempCBack)
        {
            this.CBack = tempCBack;
            this.DomainList = tempDomain;

        }
        public ThreadWithData(DataGridViewRow[] tempDataGridViewRowDomainList, CallBack tempCBack)
        {
            this.CBack = tempCBack;
            this.DataGridViewRowDomainList = tempDataGridViewRowDomainList;

        }
        public ThreadWithData(ArrayList tempDomain, CallBackEx tempCBack)
        {
            this.CBackEx = tempCBack;
            this.DomainList = tempDomain;

        }
        public void Run()
        {
            foreach (DataGridViewRow tempDomain in DomainList)
            {

                DomainStatus tempDS = uc.GetDomainStatus(tempDomain.Cells["Column2"].Value.ToString());
                CBack(tempDomain, tempDS);
                //CBack(tempDS.Status);
            }
        }
        public void RunMy()
        {
            foreach (DataGridViewRow tempDomain in DomainList)
            {

                DomainStatus tempDS = uc.GetDomainStatus(tempDomain.Cells["Column12"].Value.ToString());
                CBack(tempDomain, tempDS);
                //CBack(tempDS.Status);
            }
        }
        public void RunGrid()
        {
            foreach (DataGridViewRow tempDomain in DataGridViewRowDomainList)
            {

                DomainStatus tempDS = uc.GetDomainStatus(tempDomain.Cells["Column2"].Value.ToString());
                CBack(tempDomain, tempDS);
            }
        }
        public void RunEx()
        {
            foreach (string tempDomain in DomainList)
            {

                DomainStatus tempDS = uc.GetDomainStatus(tempDomain);
                CBackEx(tempDomain, tempDS);
                //CBack(tempDS.Status);
            }
        }
    }
    public class DomainStatus
    {
        public bool Status;
        public DateTime Create_Date;
        public DateTime Updated_Date;
        public DateTime Expiration_Date;
    }
    public class UrlCheck
    {
        public string GetHttpData(string sUrl, string charset)
        {
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(sUrl);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }

            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), System.Text.Encoding.GetEncoding(charset));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }
        //
        //Updated Date: 25-mar-2010
        //Creation Date: 23-apr-1996
        //Expiration Date: 24-apr-2019
        public DomainStatus GetDomainStatus(string domain)
        {
            DomainStatus tempDomainStatus = new DomainStatus();
            string tempContent = "";
            try
            {
                tempContent= GetHttpData("http://whois.hichina.com/cgi-bin/whois?domain=" + domain, "gb2312");
            }
            catch (Exception ex)
            {
                tempContent = "";
               
            }
            string[] tempCdate = GetRegValue(tempContent, "Creation Date:(?<cdate>([^\\n]+))", "cdate", true);

            if (tempCdate.Length > 0)
            {
                string[] tempUpdate = GetRegValue(tempContent, "Updated Date:(?<udate>([^\\n]+))", "udate", true);
                string[] tempEdate = GetRegValue(tempContent, "Expiration Date:(?<edate>([^\\n]+))", "edate", true);
                DateTime dt1 = DateTime.Parse(tempCdate[0]);
                DateTime dt2 = DateTime.Parse(tempUpdate[0]);
                DateTime dt3 = DateTime.Parse(tempEdate[0]);
                tempDomainStatus.Status = true;
                tempDomainStatus.Create_Date = dt1;
                tempDomainStatus.Updated_Date = dt2;
                tempDomainStatus.Expiration_Date = dt3;
            }
            else
            {
                tempDomainStatus.Status = false;
            }
            return tempDomainStatus;

        }
        /// <summary>
        /// 正则表达式取值
        /// </summary>
        /// <param name="HtmlCode">源码</param>
        /// <param name="RegexString">正则表达式</param>
        /// <param name="GroupKey">正则表达式分组关键字</param>
        /// <param name="RightToLeft">是否从右到左</param>
        /// <returns></returns>
        public string[] GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
        {
            MatchCollection m;
            Regex r;
            if (RightToLeft == true)
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
            }
            else
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            }
            m = r.Matches(HtmlCode);
            string[] MatchValue = new string[m.Count];
            for (int i = 0; i < m.Count; i++)
            {
                MatchValue[i] = m[i].Groups[GroupKey].Value;
            }
            return MatchValue;
        }
        public string[] GetRegValue1(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
        {
            MatchCollection m;
            Regex r;
            if (RightToLeft == true)
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
            }
            else
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            }
            m = r.Matches(HtmlCode);
            string[] MatchValue = new string[m.Count];
            for (int i = 0; i < m.Count; i++)
            {
                MatchValue[i] = m[i].Groups[GroupKey].Value;
            }
            return MatchValue;
        }

    }
}

posted on 2011-08-31 19:04 sanmao 阅读(459) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(5)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜