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

WEB邮件程序

开发平台:

Visual C++

  1. /*
  2.  * 作者:方宏俊
  3.  * 日期:2008-10-23
  4.  * 描述:获取  MSN Live 好友列表
  5.  * *                  */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Net;
  9. using System.Text;
  10. using System.Xml;
  11. namespace Fhz.Msn.OpenContactsNet
  12. {
  13.     /// <summary>
  14.     /// MSN Live 提取类
  15.     /// </summary>
  16.     public class LiveExtract : IMailContactExtract
  17.     {
  18.         #region IMailContactExtract Members
  19.         public bool Extract( NetworkCredential credential, out MailContactList list )
  20.         {
  21.             list = new MailContactList();
  22.             bool result = false;
  23.             try
  24.             {
  25.                 TicketAcquirer ticketAcquirer = new TicketAcquirer();
  26.                 string ticket = ticketAcquirer.GetTicket( credential );
  27.                 if ( string.IsNullOrEmpty( ticket ) )
  28.                 {
  29.                     return false;
  30.                 }
  31.                 UriBuilder urib = new UriBuilder();
  32.                 urib.Scheme = "HTTPS";
  33.                 urib.Path = string.Format( "/{0}/LiveContacts", credential.UserName );
  34.                 urib.Host = "cumulus.services.live.com";
  35.                 urib.Port = 443;
  36.                 HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( urib.Uri );
  37.                 string authHeader = string.Format( "WLID1.0 t="{0}"", ticket );
  38.                 request.Headers.Add( "Authorization", authHeader );
  39.                 WebResponse response = request.GetResponse();
  40.                 if ( response.ContentLength != 0 )
  41.                 {
  42.                     XmlDocument xmlDocument = new XmlDocument();
  43.                     xmlDocument.Load( response.GetResponseStream() );
  44.                     XmlNodeList contacts = xmlDocument.SelectNodes( "/LiveContacts/Contacts/Contact" );
  45.                     foreach ( XmlNode node in contacts )
  46.                     {
  47.                         XmlNode firstName = node.SelectSingleNode("Profiles/Personal/DisplayName");
  48.                         XmlNode firstMail = node.SelectSingleNode("Emails/Email/Address");
  49.                         MailContact mailContact = new MailContact();
  50.                         mailContact.Name = string.Format( "{0}", firstName.InnerText);
  51.                         mailContact.Email = firstMail == null ? string.Empty : firstMail.InnerText;
  52.                         list.Add( mailContact );
  53.                     }
  54.                 }
  55.                 result = true;
  56.             }
  57.             catch(Exception ex)
  58.             {
  59.                 string a = ex.Message;
  60.                 return false;
  61.             }
  62.             return result;
  63.         }
  64.         #endregion
  65.     }
  66. }