none
Importing Yahoo Contacts using C# RRS feed

  • Question

  • Hi,

    I have following code for import yahoo contacts from address book, but its import only top 30 contacts not all contacts, so help me to figure out this problem.

    private const string _addressBookUrl = "http://address.mail.yahoo.com/allcontacts";
            //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";



            public ActionResult Yahoo()
            {
               List<ContactDetails> lst = new List<ContactDetails>();
               lst = GetYahooContacts("UserId", "Password");
               
                return View(lst);
            }


            public List<ContactDetails> GetYahooContacts(string _username, string _pass)
            {
                try
                {
                    WebClient webclient = new WebClient();
                    webclient.Headers[HttpRequestHeader.UserAgent] = _userAgent;
                    webclient.Encoding = Encoding.UTF8;

                    byte[] firstResponse = webclient.DownloadData(_loginPage);                
                    string firstRes = Encoding.UTF8.GetString(firstResponse);

                    NameValueCollection postToLogin = new NameValueCollection();
                    Regex regex = new Regex("type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase);
                    Match 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");

                    string login = _username;
                    postToLogin.Add("login", login);
                    postToLogin.Add("passwd", _pass);

                    webclient.Headers[HttpRequestHeader.UserAgent] = _userAgent;
                    webclient.Headers[HttpRequestHeader.Referer] = _loginPage;
                    webclient.Encoding = Encoding.UTF8;
                    webclient.Headers[HttpRequestHeader.Cookie] = webclient.ResponseHeaders[HttpResponseHeader.SetCookie];

                    webclient.UploadValues(_authUrl, postToLogin);
                    
                    string cookie = webclient.ResponseHeaders[HttpResponseHeader.SetCookie];


                    string newCookie = string.Empty;
                    string[] tmp1 = cookie.Split(',');
                    foreach (string var in tmp1)
                    {
                        string[] tmp2 = var.Split(';');
                        newCookie = String.IsNullOrEmpty(newCookie) ? tmp2[0] : newCookie + ";" + tmp2[0];
                    }

                    webclient.Headers[HttpRequestHeader.Cookie] = newCookie;
                    byte[] thirdResponse = webclient.DownloadData(_addressBookUrl);                
                    string thirdRes = Encoding.UTF8.GetString(thirdResponse);
                    List<ContactDetails> lst = new List<ContactDetails>();
                    lst = ParseYahooResponse(thirdRes);
                    return lst;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            private List<ContactDetails> ParseYahooResponse(string _response)
            {
                List<ContactDetails> ContactList = new List<ContactDetails>();
                _response = _response.Substring(_response.IndexOf("InitialContacts"), (_response.IndexOf("InitialBucket") - _response.IndexOf("InitialContacts")));

                List<YahooContacts> Contacts = JsonConvert.DeserializeObject<List<YahooContacts>>(_response.Substring(_response.IndexOf('['), (_response.LastIndexOf(']') - _response.IndexOf('[')) + 1));

                foreach (YahooContacts oYahooContacts in Contacts)
                {
                    ContactDetails ocontacts = new ContactDetails();
                    ocontacts.Name = Convert.ToString(oYahooContacts.ContactName);
                    ocontacts.EmailAddress = Convert.ToString(oYahooContacts.Email);
                    ContactList.Add(ocontacts);

                }
                return ContactList;
            }
        }

    public class YahooContacts
        {
            private string contactId;
            private string contactName;
            private string email;


            public string ContactID
            {
                get { return contactId; }
                set { contactId = value; }
            }

            public string ContactName
            {
                get { return contactName; }
                set { contactName = value; }
            }

            public string Email
            {
                get { return email; }
                set { email = value; }
            }
        }

    Tuesday, April 23, 2013 7:42 AM

All replies