asp.net抓取126邮箱联系人代码

作者:袖梨 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.Text.RegularExpressions;
using System.Xml;

namespace GetEmailAddress
{
    public class Address126
    {
        //邮箱入口定义
        const string mail126 = "http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1";
        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 Address126(string name, string pwd, GetEmailAddress.Address163.Entrys type)
        {
            this.uName = name;
            this.pwd = pwd;
            en = type;
            //记录登陆邮箱的用户名和密码
            StringBuilder sb = new StringBuilder();
            sb.Append("&domain=126.com");//域
            sb.Append("&language=-1");//语言
            sb.Append("&bCookie=");
            sb.Append("&username=" + name);
            sb.Append("&savelogin=");
            sb.Append("url2=http://mail.126.com/errorpage/err_126.htm");
            sb.Append("&user=" + name.Substring(0,name.IndexOf("@")));//登录名
            sb.Append("&password=" + pwd);//密码
            sb.Append("&style=-1"); //样式
            sb.Append("&secure=");
            b = Encoding.ASCII.GetBytes(sb.ToString());
        }
        ///
        /// 得到网页数据
        ///

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

        /// 解析地址
        ///
        private string Process126mail(string EntryUrl)
        {
            try
            {
                #region//第一次请求登陆地址
                //第一请求126登陆邮箱地址 "http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1";
                //获取请求的内容
                HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(new Uri(EntryUrl));
                hwr.Method = "POST";
                hwr.KeepAlive = false;
                hwr.ContentType = "application/x-www-form-urlencoded";
                hwr.ContentLength = b.Length;
                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();
                }

                reg = new Regex(@"HTTP-EQUIV=REFRESH CONTENT=""[d];URL=(.+?)"">");
                m = reg.Match(rb.ToUpper());
                if (m.Success)
                {
                    EntryUrl = m.Groups[1].Value.ToLower();
                }

                EntryUrl = TransCode(EntryUrl);

                //把用户名和密码添加到头部cookies
                Uri nurl1 = new Uri("http://www.163.com");
                Uri nurl2 = new Uri("http://reg.163.com");
                Uri nurl3 = new Uri(EntryUrl);
                foreach (System.Net.Cookie cookie in cookieCon.GetCookies(nurl1))
                {
                    cookie.Domain = nurl3.Host;
                }
                cookieCon.Add(cookieCon.GetCookies(nurl1));

                foreach (System.Net.Cookie cookie in cookieCon.GetCookies(nurl2))
                {
                    cookie.Domain = nurl3.Host;
                }
                cookieCon.Add(cookieCon.GetCookies(nurl2));

                foreach (System.Net.Cookie cookie in cookieCon.GetCookies(nurl3))
                {
                    cookie.Domain = ".126.com";
                    cookie.Expires = DateTime.Now.AddHours(1);
                }
                cookieCon.Add(cookieCon.GetCookies(nurl3));
                #endregion

                //忽略第二次请求新地址
         
                #region//第三次请求新地址
                //根据第二个地址获取第三个地址
            
                string rUrl = string.Empty;
                string threrrUrl = "http://entry.mail.126.com/cgi/ntesdoor?username=" + uName + "&hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1";
                hwr = (HttpWebRequest)HttpWebRequest.Create(new Uri(threrrUrl));
                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;
                hwr.Method = "HEAD";
                hwr.AllowAutoRedirect = false;

                // 获取返回信息
                using (HttpWebResponse wr = (HttpWebResponse)hwr.GetResponse())
                {
                    rUrl = wr.GetResponseHeader("Location");   //获取到rurl http://cwebmail.mail.126.com/js4/main.jsp?sid=WBGSoZlBkEKZafrfivBBqbkxsHBowIPx
                }
                #endregion

                #region////第四次请求新地址获得数据
                //根据第三个地址获取到http://cwebmail.mail.126.com/js4/s?sid=WBGSoZlBkEKZafrfivBBqbkxsHBowIPx&func=global:sequential&from=nav&action=showHideFolder&showAd=false&userType=newuser&uid=xxxx@sina.com

                //然后请求分析获取联系人存为XML数据
                string post = "pab:searchContactsFNfalsetruepab:getAllGroups";
                byte[] pb = Encoding.ASCII.GetBytes(post.ToString());
                string bookurl = (rUrl + "&func=global:sequential").Replace("js3", "a");
                hwr = (HttpWebRequest)HttpWebRequest.Create(new Uri(bookurl.Replace("main.jsp", "s")));
                hwr.Method = "POST";
                hwr.ContentType = "application/xml";
                hwr.ContentLength = pb.Length;
                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(pb, 0, pb.Length);
                }

                // 获取返回信息
                using (HttpWebResponse wr = (HttpWebResponse)hwr.GetResponse())
                {
                    StreamReader sr = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
                    rb = sr.ReadToEnd();
                    sr.Close();
                }
                #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.mail126:
                    EntryUrl = mail126;
                    break;
                default:
                    break;
            }
            return string.Format(EntryUrl, uName, pwd);
        }
        private Address126()
        {
        } //封闭接口

    }
}

相关文章

精彩推荐