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

 代码如下 复制代码

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;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">邮箱名称</param>
        /// <param name="pwd">邮箱密码</param>
        /// <param name="type"></param>
        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() );
        }
        /// <summary>
        /// 得到网页数据
        /// </summary>
        /// <returns>得到网页HTML数据</returns>
        private string GetHtml()
        {
            string EntryUrl = GetEntryUrl();
         
            return Process126mail( EntryUrl );
        }
        /// <summary>
        /// 分析126
        /// </summary>
        /// <param name="EntryUrl">解析地址</param>
        /// <returns></returns>
        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();
        }
        /// <summary>
        /// 得到126通讯录的内容
        /// </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();
            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>
        /// <returns>得到请求地址</returns>
        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()
        {
        } //封闭接口

    }
}

时间: 2024-11-05 22:48:53

asp.net抓取QQ邮箱联系人实现代码的相关文章

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

 代码如下 复制代码 using System; using System.Net; using System.IO; using System.Text; using System.Collections.Specialized; using System.Text.RegularExpressions; /*  * AddressSina  * 新浪免费邮箱获取联系人(sina.com/sina.cn)  *  *  改?履?s  * ver 1.00.00 @[20100721]  星缘梦

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      {        

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

 代码如下 复制代码 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 {     p

asp采集抓取网上房产信息的代码_小偷/采集

复制代码 代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!-- #include file="conn.asp" --> <!-- #include file="inc/function.asp" --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&

ASP.NET抓取网页内容的实现方法

 这篇文章主要介绍了ASP.NET抓取网页内容的实现方法,涉及使用HttpWebRequest及WebResponse抓取网页内容的技巧,需要的朋友可以参考下     本文实例讲述了ASP.NET抓取网页内容的实现方法.分享给大家供大家参考.具体实现方法如下: 一.ASP.NET 使用HttpWebRequest抓取网页内容 代码如下: /// <summary>方法一:比较推荐 /// 用HttpWebRequest取得网页源码 /// 对于带BOM的网页很有效,不管是什么编码都能正确识别

php使用curl抓取qq空间的访客信息示例

 这篇文章主要介绍了php使用curl抓取qq空间的访客信息示例,需要的朋友可以参考下 config.php  代码如下: <?php define('APP_DIR', dirname(__FILE__)); define('COOKIE_FILE', APP_DIR . '/app.cookie.txt'); //会话记录文件 define('VISITOR_CAPTURE_INTERVAL', 3); //QQ采集间隔 define('VISITOR_DATA_UPLOAD_INTERVA

ASP.NET抓取网页内容

原文:ASP.NET抓取网页内容 一.ASP.NET 使用HttpWebRequest抓取网页内容   这种方式抓取某些页面会失败 不过,有时候我们会发现,这个程序在抓取某些页面时,是获不到所需的内容的,有时候甚至返回404的错误提示页,这是什么原因呢? 其实,很多人都忽略了一个问题,那就是服务器默认的浏览器问题.有的服务器默认的浏览器是手机浏览器,那么,当我抓取这个服务器下的网页是,就相当于用手机浏览器来打开网页,而如果要抓取的目标网页没有相应的手机网页的话,就会返回意想不到的结果,有的返回4

java使用正则抓取网页邮箱_java

使用正则抓捕网上邮箱 这就是我们需要抓捕的网站. 实现思路: 1.使用java.net.URL对象,绑定网络上某一个网页的地址 2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象 3.通过HttpConnection对象的getInputStream()方法获得该网络文件的输入流对象InputStream 4.循环读取流中的每一行数据,并由Pattern对象编译的正则表达式区配每一行字符,取得email地址 下面是我们的代码: pa

ASP.NET抓取网页内容的实现方法_实用技巧

本文实例讲述了ASP.NET抓取网页内容的实现方法.分享给大家供大家参考.具体实现方法如下: 一.ASP.NET 使用HttpWebRequest抓取网页内容 复制代码 代码如下: /// <summary>方法一:比较推荐  /// 用HttpWebRequest取得网页源码  /// 对于带BOM的网页很有效,不管是什么编码都能正确识别  /// </summary>  /// <param name="url">网页地址" </