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

WEB邮件程序

开发平台:

Visual C++

  1. //Code by Gnilly (http://gnillydev.blogspot.com)
  2. //Offical site: http://opencontacts.sourceforge.net
  3. //This program is free software: you can redistribute it and/or modify
  4. //it under the terms of the GNU Lesser General Public License as published by
  5. //the Free Software Foundation, either version 3 of the License, or
  6. //(at your option) any later version.
  7. //This program is distributed in the hope that it will be useful,
  8. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. //GNU Lesser General Public License for more details.
  11. //You should have received a copy of the GNU Lesser General Public License
  12. //along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Net;
  16. using System.Text;
  17. using System.Xml;
  18. namespace OpenContactsNet
  19. {
  20.     public class LiveExtract : IMailContactExtract
  21.     {
  22.         #region IMailContactExtract Members
  23.         public bool Extract( NetworkCredential credential, out MailContactList list )
  24.         {
  25.             list = new MailContactList();
  26.             bool result = false;
  27.             try
  28.             {
  29.                 TicketAcquirer ticketAcquirer = new TicketAcquirer();
  30.                 string ticket = ticketAcquirer.GetTicket( credential );
  31.                 if ( string.IsNullOrEmpty( ticket ) )
  32.                 {
  33.                     return false;
  34.                 }
  35.                 UriBuilder urib = new UriBuilder();
  36.                 urib.Scheme = "HTTPS";
  37.                 urib.Path = string.Format( "/{0}/LiveContacts", credential.UserName );
  38.                 urib.Host = "cumulus.services.live.com";
  39.                 urib.Port = 443;
  40.                 HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( urib.Uri );
  41.                 string authHeader = string.Format( "WLID1.0 t="{0}"", ticket );
  42.                 request.Headers.Add( "Authorization", authHeader );
  43.                 WebResponse response = request.GetResponse();
  44.                 if ( response.ContentLength != 0 )
  45.                 {
  46.                     XmlDocument xmlDocument = new XmlDocument();
  47.                     xmlDocument.Load( response.GetResponseStream() );
  48.                     XmlNodeList contacts = xmlDocument.SelectNodes( "/LiveContacts/Contacts/Contact" );
  49.                     foreach ( XmlNode node in contacts )
  50.                     {
  51.                         XmlNode firstName = node.SelectSingleNode("Profiles/Personal/DisplayName");
  52.                         //XmlNode lastName = node.SelectSingleNode("Profiles/Personal/DisplayName");
  53.                         XmlNode firstMail = node.SelectSingleNode("Emails/Email/Address");
  54.                         MailContact mailContact = new MailContact();
  55.                         mailContact.Name = string.Format( "{0}", firstName.InnerText);
  56.                         mailContact.Email = firstMail == null ? string.Empty : firstMail.InnerText;
  57.                         list.Add( mailContact );
  58.                     }
  59.                 }
  60.                 result = true;
  61.             }
  62.             catch(Exception ex)
  63.             {
  64.                 string a = ex.Message;
  65.                 return false;
  66.             }
  67.             return result;
  68.         }
  69.         #endregion
  70.     }
  71. }