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

作者:袖梨 2022-06-25
 代码如下 复制代码


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace GetEmailAddress
{
    public  class Address163 
    {
        //邮箱入口定义
        const string mail_163_com ="https://reg.163.com/logins.jsp?username={0}&password={1}&type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1";   //判断用户名和密码是否为空/正确
        protected static string cookieheader = string.Empty;    //定义公共的 Cookie Header 变量
        protected static string NextUrl = string.Empty;   //定义下次访问的Url变量
        CookieContainer cookieCon = new CookieContainer();
        HttpWebRequest req;
        HttpWebResponse res;
        private string  uName;
        private string pwd;
        private Entrys en;
        ///


        /// 构造函数
        ///

        /// 邮箱名称
        /// 邮箱密码
        ///
        public Address163(string name,string pwd,Entrys type)
        {
            this.uName = name;
            this.pwd = pwd;
            en=type;
        }
        ///
        /// 得到网页数据
        ///

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

        /// 解析地址
        ///
        private string Process163mail(string EntryUrl)
        {
            try
            {
                //#1
                string ReturnHtml = GetRequestHtml(EntryUrl, Encoding.GetEncoding("utf-8"));
                NextUrl = ReturnHtml.Substring(ReturnHtml.IndexOf("URL=") + 6);
                NextUrl = NextUrl.Substring(0, NextUrl.IndexOf("""));
              
                string[] arr = NextUrl.Split(new string[] { "&#" }, StringSplitOptions.RemoveEmptyEntries);
                string str1 = string.Empty;
                for (int i = 0; i < arr.Length; i++)
                {
                    int j = int.Parse(arr[i]);
                    str1 += Encoding.ASCII.GetString(new byte[] { (byte)j });//str1
                }
                NextUrl = str1; 
     
                //忽略第二次请求地址
       
                //NextUrl = str2; 
                //#3
                NextUrl = "http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=35&username="+uName.Replace("@163.com","");
                ReturnHtml = GetRequestHtml(NextUrl, Encoding.GetEncoding("gb2312"));
                //改向到 http://cg1a181.mail.163.com/js3/main.jsp?sid=eELXghnSsySYEzpNbLSSrMNUaUSOCRib 由服务器造成
                string post = "pab:searchContactsFNtrueuser:getSignaturespab:getAllGroups";
                byte[] pb = Encoding.ASCII.GetBytes( post.ToString() );

                NextUrl = res.ResponseUri.AbsoluteUri;

                string bookurl = ( NextUrl + "&func=global:sequential" ).Replace( "js3", "a" );
                req = ( HttpWebRequest ) HttpWebRequest.Create( new Uri( bookurl.Replace( "main.jsp", "s" ) ) );
                req.Method = "POST";
                req.ContentType = "application/xml";
                req.ContentLength = pb.Length;
                req.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)";
                req.CookieContainer = cookieCon;

                // 发送数据
                using( Stream s = req.GetRequestStream() )
                {
                    s.Write( pb, 0, pb.Length );
                }

                // 获取返回信息
                using( HttpWebResponse wr = ( HttpWebResponse ) req.GetResponse() )
                {
                    StreamReader sr = new StreamReader( wr.GetResponseStream(), Encoding.UTF8 );
                    ReturnHtml = sr.ReadToEnd();
                    sr.Close();
                }
                return ReturnHtml;
                
            }
            catch (Exception ex)
            {
                return ex.ToString()+"登录失败,请检查用户名称和密码";
                 
            }
        }

        ///


        /// 提交到服务器
        ///

        /// 要提交的URL
        /// 返回HTML内容
        private string GetRequestHtml(string URL)
        {
            return GetRequestHtml(URL, Encoding.Default);
        }
        private string GetRequestHtml(string URL, Encoding EnCodeing)
        {
            return GetRequestHtml(URL,EnCodeing ,ReqMethod.POST);//Encoding.Default
        }
        private string GetRequestHtml(string URL, Encoding EnCodeing,ReqMethod RMethod)
        {
            string html = string.Empty;
            try
            {
                req = (HttpWebRequest)WebRequest.Create(URL);
                req.AllowAutoRedirect = true;
                req.CookieContainer = cookieCon;
                req.Credentials = CredentialCache.DefaultCredentials;
                req.Method = RMethod.ToString();
                req.ContentType = "application/x-www-form-urlencoded";
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; Mozilla/4.0(Compatible Mozilla/4.0(Compatible-EmbeddedWB 14.59 http://bsalsa.com/ EmbeddedWB- 14.59  from: http://bsalsa.com/ ; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30618; CIBA)";

                res = (HttpWebResponse)req.GetResponse();

                if (cookieheader.Equals(string.Empty))
                {
                    cookieheader = req.CookieContainer.GetCookieHeader(new Uri(URL));
                }
                else
                {
                    req.CookieContainer.SetCookies(new Uri(URL), cookieheader);
                }

                html= new StreamReader(res.GetResponseStream(), EnCodeing).ReadToEnd();
            }
            catch (Exception ex)
            {
                html = ex.Message;
            }
            return html;
        }

        ///


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

        /// 通讯录集合
        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();
            if (!string.IsNullOrEmpty(resHtml))
            {
                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
        }
        ///
        /// 枚举获取入口地址
        ///

        public enum Entrys {
            mail163,
            mail126,
            mailQQ,
            mailYeah
        }
        ///
        /// 得到请求地址
        ///

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

    }
}

相关文章

精彩推荐