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


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;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">邮箱名称</param>
        /// <param name="pwd">邮箱密码</param>
        /// <param name="type"></param>
        public Address163(string name,string pwd,Entrys type)
        {
            this.uName = name;
            this.pwd = pwd;
            en=type;
        }
        /// <summary>
        /// 得到网页数据
        /// </summary>
        /// <returns>得到网页HTML数据</returns>
        private  string GetHtml()
        {            
            string EntryUrl = GetEntryUrl();
            return Process163mail(EntryUrl);
        }      
        /// <summary>
        /// 分析163
        /// </summary>
        /// <param name="EntryUrl">解析地址</param>
        /// <returns></returns>
        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 = "<?xml version="1.0"?><object><array name="items"><object><string name="func">pab:searchContacts</string><object name="var"><array name="order"><object><string name="field">FN</string><boolean name="ignoreCase">true</boolean></object></array></object></object><object><string name="func">user:getSignatures</string></object><object><string name="func">pab:getAllGroups</string></object></array></object>";
                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()+"登录失败,请检查用户名称和密码";
                 
            }
        }
        /// <summary>
        /// 提交到服务器
        /// </summary>
        /// <param name="URL">要提交的URL</param>
        /// <returns>返回HTML内容</returns>
        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;
        }
        /// <summary>
        /// 得到163通讯录的内容
        /// </summary>
        /// <returns>通讯录集合</returns>
        public List<Person> getContact()
        {
            List<Person> ls = new List<Person>();
            //读取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;
        }
        /// <summary>
        /// 枚举获请求用什么方式
        /// </summary>
        private enum ReqMethod
        {
            POST,
            GET
        }
        /// <summary>
        /// 枚举获取入口地址
        /// </summary>
        public enum Entrys {
            mail163,
            mail126,
            mailQQ,
            mailYeah
        }
        /// <summary>
        /// 得到请求地址
        /// </summary>
        /// <returns>得到请求地址</returns>
        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() { } //封闭接口
    }
}

你可能感兴趣的:(asp.net)