YahooExtract.cs
上传用户:asz878
上传日期:2020-04-01
资源大小:189k
文件大小:6k
源码类别:

WEB邮件程序

开发平台:

Visual C++

  1. /*
  2.  * 作者:方宏俊
  3.  * 日期:2008-10-23
  4.  * 描述:获取 Yahoo 好友列表
  5.  * *                  */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.Specialized;
  9. using System.Net;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. namespace Fhz.Msn.OpenContactsNet
  13. {
  14.     /// <summary>
  15.     /// Yahoo 好友列表
  16.     /// </summary>
  17.     public class YahooExtract : IMailContactExtract
  18.     {
  19.         private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";
  20.         private const string _authUrl = "https://login.yahoo.com/config/login?";
  21.         private const string _loginPage = "https://login.yahoo.com/config/login";
  22.         private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
  23.         #region IMailContactExtract Members
  24.         public bool Extract( NetworkCredential credential, out MailContactList list )
  25.         {
  26.             bool result = false;
  27.             list = new MailContactList();
  28.             try
  29.             {
  30.                 WebClient webClient = new WebClient();
  31.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  32.                 webClient.Encoding = Encoding.UTF8;
  33.                 byte[] firstResponse = webClient.DownloadData( _loginPage );
  34.                 string firstRes = Encoding.UTF8.GetString( firstResponse );
  35.                 NameValueCollection postToLogin = new NameValueCollection();
  36.                 Regex regex = new Regex( "type="hidden" name="(.*?)" value="(.*?)"", RegexOptions.IgnoreCase );
  37.                 Match match = regex.Match( firstRes );
  38.                 while ( match.Success )
  39.                 {
  40.                     if ( match.Groups[ 0 ].Value.Length > 0 )
  41.                     {
  42.                         postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );
  43.                     }
  44.                     match = regex.Match( firstRes, match.Index + match.Length );
  45.                 }
  46.                 postToLogin.Add( ".save", "Sign In" );
  47.                 postToLogin.Add( ".persistent", "y" );
  48.                 string login = credential.UserName.Split( '@' )[ 0 ];
  49.                 postToLogin.Add( "login", login );
  50.                 postToLogin.Add( "passwd", credential.Password );
  51.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  52.                 webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;
  53.                 webClient.Encoding = Encoding.UTF8;
  54.                 webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];
  55.                 webClient.UploadValues( _authUrl, postToLogin );
  56.                 string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];
  57.                 if ( string.IsNullOrEmpty( cookie ) )
  58.                 {
  59.                     return false;
  60.                 }
  61.                 string newCookie = string.Empty;
  62.                 string[] tmp1 = cookie.Split( ',' );
  63.                 foreach ( string var in tmp1 )
  64.                 {
  65.                     string[] tmp2 = var.Split( ';' );
  66.                     newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];
  67.                 }
  68.                 // set login cookie
  69.                 webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
  70.                 byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
  71.                 string thirdRes = Encoding.UTF8.GetString( thirdResponse );
  72.                 string crumb = string.Empty;
  73.                 Regex regexCrumb = new Regex( "type="hidden" name="\.crumb" id="crumb1" value="(.*?)"", RegexOptions.IgnoreCase );
  74.                 match = regexCrumb.Match( thirdRes );
  75.                 if ( match.Success && match.Groups[ 0 ].Value.Length > 0 )
  76.                 {
  77.                     crumb = match.Groups[ 1 ].Value;
  78.                 }
  79.                 NameValueCollection postDataAB = new NameValueCollection();
  80.                 postDataAB.Add( ".crumb", crumb );
  81.                 postDataAB.Add( "vcp", "import_export" );
  82.                 postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );
  83.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  84.                 webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;
  85.                 byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
  86.                 string csvData = Encoding.UTF8.GetString( FourResponse );
  87.                 string[] lines = csvData.Split( 'n' );
  88.                 foreach ( string line in lines )
  89.                 {
  90.                     string[] items = line.Split( ',' );
  91.                     if ( items.Length < 5 )
  92.                     {
  93.                         continue;
  94.                     }
  95.                     string email = items[ 4 ];
  96.                     string name = items[ 3 ];
  97.                     if ( !string.IsNullOrEmpty( email ) && !string.IsNullOrEmpty( name ) )
  98.                     {
  99.                         email = email.Trim( '"' );
  100.                         name = name.Trim( '"' );
  101.                         if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )
  102.                         {
  103.                             MailContact mailContact = new MailContact();
  104.                             mailContact.Name = name;
  105.                             mailContact.Email = email;
  106.                             list.Add( mailContact );
  107.                         }
  108.                     }
  109.                 }
  110.                 result = true;
  111.             }
  112.             catch
  113.             {
  114.             }
  115.             return result;
  116.         }
  117.         #endregion
  118.     }
  119. }