my favorite

2008/03/27

ASP.NET 取得 Yahoo! 的聯絡人清單 (Get Yahoo Mail Contacts)

找了快兩天,還跟meeya的學長聯繫上,可是自己功力不夠還是寫不出來

就在快放棄之餘,再度google了一下,總算給我找到一個solution

其實這個方法自己也試過,不過一直以為是錯的,就沒有再繼續研究下去

網路上果然是有很多高手的,擷取Yahoo mail contact的解法我就直接附上連結了

程式碼大致做的事情可以歸納如下

  1. 連結至Login(https://login.yahoo.com/config/login)頁面擷取資訊
  2. 帶入Login資訊(login=UserName, passwd=Password)
  3. 取得cookie,把逗號 , 改為分號 ; 再把cookie代入WebClient instance
  4. 從yahoo提供的連絡人清單檔案(Yahoo_ab.csv)取得相關資訊,URL為http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv
  5. 解析Yahoo_ab.csv (以目前來說總共有55個欄位)
最後,Gnilly提供一個MailContact類別以儲存nickname及email (MailContact裡的name就是nick,而不是姓氏或名字。我重新修改了一些以符合實際的應用,程式碼如下



/// <summary>
/// Yahoo! mail contact information
/// </summary>
/// <see cref="http://gnillydev.blogspot.com/2007/10/yahoo-contact-import-class-in-c.html"/>
public class MailContact {
private string email = string.Empty;
private string nickName = string.Empty;
private string firstName = string.Empty;
private string secondName = string.Empty;
private string fullName = string.Empty;

/// <summary>
/// nickname of contact
/// </summary>
public string NickName {
get {
return this.nickName;
}
set {
this.nickName = value;
}
}

/// <summary>
/// email of contact
/// </summary>
public string Email {
get {
return this.email;
}
set {
this.email = value;
}
}

/// <summary>
/// first name of contact
/// </summary>
public string FirstName {
get {
return this.firstName;
}
set {
this.firstName = value;
}
}

/// <summary>
/// second name of contact
/// </summary>
public string SecondName {
get {
return this.secondName;
}
set {
this.secondName = value;
}
}

/// <summary>
/// full name of contact ( string concatenation of first name and second name )
/// </summary>
public string FullName {
get {
return String.Concat(this.firstName, this.secondName);
}
}
}

public class MailContactList : List<MailContact> {
}



在Extract方法裡要修改一些段落,還算挺簡單
在方法的最後一段把值填入MailContact類別的地方,增加FirstName, SecondName, FullName這些欄位就可以了。當然這些值分別要取自Yahoo_ab.csv,可以看下列幾行程式碼

string email = items[4];
string nickName = items[3];
string firstName = items[2];
string secondName = items[0];

最後我把結果以GridView呈現,並且增加是否要去除重複的資料列,如下圖


只能說程式真的還是高手才能當飯吃,我只好來撿撿高手旁邊掉了的碎末吃吃 Q_Q

No comments:

BIO

Taipei, GuTing, Taiwan

huang47 | personal

huang47 | personal