|
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
public partial class YahooContact : System.Web.UI.Page{
// Declare the private variables
private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";
private const string _authUrl = "https://login.yahoo.com/config/login?";
private const string _loginPage = "https://login.yahoo.com/config/login";
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";
private string username = string.Empty;
private string password = string.Empty;
/// <summary>
/// To Extract the Address Book
/// </summary>
/// <param name="uname"></param>
/// <param name="upass"></param>
/// <returns></returns>
private ArrayList Extract(string uname, string upass){
// Declare variables
ArrayList addressArray = null;
WebClient webClient = null;
WebProxy we = null;
byte[] firstResponse = null;
byte[] thirdResponse = null;
string[] lines = null;
string[] tmp1 = null;
NameValueCollection postToLogin = null;
Match match = null;
Regex regex = null;
Regex regexCrumb = null;
NameValueCollection postDataAB = null;
byte[] FourResponse = null;
string login = string.Empty;
string cookie = string.Empty;
string newCookie = string.Empty;
string thirdRes = string.Empty;
string crumb = string.Empty;
string csvData = string.Empty;
string firstRes = string.Empty;
bool result = false;
addressArray = new ArrayList();
try {
// Create webClient object to get the methods for
// sending data to and receiving data from a resource identified by a URI.
webClient = new WebClient();
// Set the useragent info in the header
webClient.Headers[HttpRequestHeader.UserAgent] = _userAgent;
webClient.Encoding = Encoding.UTF8;
// Set the Proxy setting
we = new WebProxy("ngproxy.Persistent.co.in:8080");
we.UseDefaultCredentials = true;
webClient.Proxy = we;
// Download the login page.
firstResponse = webClient.DownloadData(_loginPage);
firstRes = Encoding.UTF8.GetString(firstResponse);
// Create the name value collection which we have to send with
// login info such as user name and password
postToLogin = new NameValueCollection();
// By the Regular Expression Extact the desire name value collection
regex = new Regex("type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase);
match = regex.Match(firstRes);
while (match.Success){
if (match.Groups[0].Value.Length > 0){
postToLogin.Add(match.Groups[1].Value, match.Groups[2].Value);
}
match = regex.Match(firstRes, match.Index + match.Length);
}
postToLogin.Add(".save", "Sign In");
postToLogin.Add(".persistent", "y");
login = uname.Split('@').GetValue(0).ToString();
postToLogin.Add("login", login);
postToLogin.Add("passwd", upass);
// Again set the user agent and other information on heeder
webClient.Headers[HttpRequestHeader.UserAgent] = _userAgent;
webClient.Headers[HttpRequestHeader.Referer] = _loginPage;
webClient.Encoding = Encoding.UTF8;
webClient.Headers[HttpRequestHeader.Cookie] = webClient.ResponseHeaders[HttpResponseHeader.SetCookie];
// Upload the page..
webClient.UploadValues(_authUrl, postToLogin);
// Get the cookies info
cookie = webClient.ResponseHeaders[HttpResponseHeader.SetCookie];
newCookie = string.Empty;
tmp1 = cookie.Split(',');
foreach (string var in tmp1){
string[] tmp2 = var.Split(';');
newCookie = (String.IsNullOrEmpty(newCookie) ? tmp2[0] : newCookie + ";" + tmp2[0]);
}
// set login cookie
webClient.Headers[HttpRequestHeader.Cookie] = newCookie;
// Download the page
thirdResponse = webClient.DownloadData(_addressBookUrl);
thirdRes = Encoding.UTF8.GetString(thirdResponse);
regexCrumb = new Regex("type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase);
match = regexCrumb.Match(thirdRes);
if (match.Success && match.Groups[0].Value.Length > 0){
crumb = match.Groups[1].Value;
}
// Create the mame value colection
postDataAB = new NameValueCollection();
postDataAB.Add(".crumb", crumb);
postDataAB.Add("vcp", "import_export");
postDataAB.Add("submit[action_export_yahoo]", "Export Now");
// Set the headers informations
webClient.Headers[HttpRequestHeader.UserAgent] = _userAgent;
webClient.Headers[HttpRequestHeader.Referer] = _addressBookUrl;
// Upload the values
FourResponse = webClient.UploadValues(_addressBookUrl, postDataAB);
csvData = Encoding.UTF8.GetString(FourResponse);
lines = csvData.Split('\n');
foreach (string line in lines){
string[] items = line.Split(',');
if (items.Length < 5){
continue;
}
string email = items[4];
string name = items[3];
if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(name)){
email = email.Trim('"');
name = name.Trim('"');
if (!email.Equals("Email") && !name.Equals("Nickname")){
MailContact mailContact = new MailContact();
mailContact.Name = name;
mailContact.Email = email;
addressArray.Add(email);
}
}
}
result = true;
}
catch(Exception ex){
throw ex;
}
return addressArray;
}
/// <summary>
/// On submit button clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSubmit_Click(object sender, EventArgs e){
ArrayList contacts = new ArrayList();
StringBuilder sb = new StringBuilder();
int j = 1;
contacts = Extract(txtusername.Text.Trim(), txtpassword.Text.Trim());
for (int i = 0; i < contacts.Count; i++){
if (contacts[i].ToString().Trim() != ""){
sb.Append("(" + j + ") " + contacts[i].ToString() + "<br>");
j++;
}
}
dvcontacts.InnerHtml = sb.ToString();
}
}
/// <summary>
/// Mail Contact Class
/// </summary>
public class MailContact{
// To store the email id
private string _email = string.Empty;
// To store the name of person
private string _name = string.Empty;
public string Name{
get { return _name; }
set { _name = value; }
}
public string Email{
get { return _email; }
set { _email = value; }
}
public string FullEmail{
get { return Email; }
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.