一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net抓取QQ邮箱联系人实现代码

时间:2022-06-25 03:58:25 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace GetEmailAddress
{
    public class AddressQQ
    {
        //邮箱入口定义
        const string mailqq = "https://mail.qq.com/cgi-bin/loginpage";
        protected static string cookieheader = string.Empty;    //定义公共的 Cookie Header 变量
        protected static string NextUrl = string.Empty;   //定义下次访问的Url变量
        CookieContainer cookieCon = new CookieContainer();

        private string uName;
        private string pwd;
        private GetEmailAddress.Address163.Entrys en;
        Regex reg;
        Match m;
        byte[] b;
        ///


        /// 构造函数
        ///

        /// 邮箱名称
        /// 邮箱密码
        ///
        public AddressQQ( string name, string pwd, GetEmailAddress.Address163.Entrys type )
        {
            this.uName = name;
            this.pwd = pwd;
            en = type;
            //记录登陆邮箱的用户名和密码
            //StringBuilder sb = new StringBuilder();
            ////sid=yKambzyz%2C2%2Czh_CN&firstlogin=false&starttime=1281074395894&redirecturl=&f=html&p=starking521&ept=0&delegate_url=& s=&ts=1281072399&from=&ppp=&chg=0&checkisWebLogin=15&uin=kongwei1314521&aliastype=@qq.com&pp=00000000000&verifycode=
            // sb.Append( "&sid=yKambzyz%2C2%2Czh_CN" );//域
            //sb.Append( "&firstlogin=false" );//语言
            //sb.Append( "&redirecturl=" );//语言
            //sb.Append( "&f=html" ); 
            //sb.Append( "&p=" + pwd );//密码
            //sb.Append( "&ept=0" );
            //sb.Append( "&delegate_url=" );
            //sb.Append( "&s=" );

            //sb.Append( "&from=" );
           
            //sb.Append( "&ppp=" );    
            //sb.Append( "&chg=0" ); //样式

            //sb.Append( "&uin=" + name.Replace( "@qq.com", "" ) );//登录名
            //sb.Append( "&aliastype=@qq.com" );

            //sb.Append( "&pp=00000000000" );

            //sb.Append( "&verifycode=" );
            //b = Encoding.ASCII.GetBytes( sb.ToString() );
        }
        ///


        /// 得到网页数据
        ///

        /// 得到网页HTML数据
        private string GetHtml()
        {
            string EntryUrl = GetEntryUrl();
         
            return Process126mail( EntryUrl );
        }
        ///
        /// 分析126
        ///

        /// 解析地址
        ///
        private string Process126mail( string EntryUrl )
        {
            try
            {
                #region//第一次请求登陆地址
                //获取请求的内容
                HttpWebRequest hwr = ( HttpWebRequest ) HttpWebRequest.Create( new Uri( EntryUrl ) );
                hwr.Method = "GET";
                hwr.ContentType = "application/x-www-form-urlencoded";
                hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; GTB6; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                hwr.CookieContainer = cookieCon;
               

                //// 发送数据
                using( Stream s = hwr.GetRequestStream() )
                {
                    s.Write( b, 0, b.Length );
                }
                string rb = string.Empty;

                // 获取返回信息
                using( HttpWebResponse wr = ( HttpWebResponse ) hwr.GetResponse() )
                {

                    StreamReader sr = new StreamReader( wr.GetResponseStream(), Encoding.UTF8 );
                    rb = sr.ReadToEnd();
                 
                    sr.Close();
                }
              
                //把用户名和密码添加到头部cookies
                Uri nurl1 = new Uri( "https://mail.qq.com/cgi-bin/loginpage" );

                foreach( System.Net.Cookie cookie in cookieCon.GetCookies( nurl1 ) )
                {

                    cookie.Domain = ".mail.qq.com";
                    cookie.Expires = DateTime.Now.AddHours( 1 );
                }
                cookieCon.Add( cookieCon.GetCookies( nurl1 ) );
                #endregion

               

                return rb;
            }
            catch( Exception ex )
            {
                return ex.ToString() + "登录失败,请检查用户名称和密码";

            }
        }
     

        //正则过滤
        private static string TransCode( string str )
        {
            Regex r = new Regex( @"&#([d]{1,5})", RegexOptions.None );
            StringBuilder s = new StringBuilder();

            foreach( Match m in r.Matches( str ) )
            {
                char c = ( char ) int.Parse( m.Groups[ 1 ].Value );
                s.Append( c.ToString() );
            }
            return s.ToString();
        }
        ///


        /// 得到126通讯录的内容
        ///

        /// 通讯录集合
        public List getContact()
        {
            List ls = new List();
            //读取XML数据然后进行    选择匹配筛选出来匹配的邮箱
            string resHtml = Encoding.UTF8.GetString( Encoding.Convert( Encoding.UTF8, Encoding.UTF8, Encoding.UTF8.GetBytes( this.GetHtml() ) ) );
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml( resHtml );

            XmlNodeList xnl = xmlDoc.SelectNodes( "/result/array/object" );
            if( xnl == null || xnl.Count <= 0 )
                return ls;

            XmlNodeList linkNOdes = xnl[ 0 ].SelectNodes( "array/object" );
            foreach( XmlNode linkNode in linkNOdes )
            {
                Person ps = new Person();
                foreach( XmlNode xn2 in linkNode.ChildNodes )
                {
                    //取得邮箱地址
                    if( xn2.Attributes[ "name" ].Value == "EMAIL;PREF" )
                    {
                        ps.Email = xn2.InnerText;
                    }
                    if( xn2.Attributes[ "name" ].Value == "FN" )
                    {
                        if( !string.IsNullOrEmpty( xn2.InnerText ) )
                        {
                            ps.Name = xn2.InnerText;
                        }
                        else
                        {
                            ps.Name = "暂无名称";
                        }
                    }
                }
                ls.Add( ps );
            }

            return ls;
        }
        ///


        /// 枚举获请求用什么方式
        ///

        private enum ReqMethod
        {
            POST,
            GET
        }
        ///
        /// 得到请求地址
        ///

        /// 得到请求地址
        private string GetEntryUrl()
        {
            string EntryUrl = string.Empty;
            switch( en )
            {
                case GetEmailAddress.Address163.Entrys.mailQQ:
                    EntryUrl = mailqq;
                    break;
                default:
                    break;
            }
            return string.Format( EntryUrl, uName, pwd );
        }
        private AddressQQ()
        {
        } //封闭接口

    }
}

热门栏目