使用C#导入Yahoo联系人 [英] Importing Yahoo Contacts using C#

查看:80
本文介绍了使用C#导入Yahoo联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自地址簿的导入yahoo联系人的以下代码,但是它只导入前30位联系人而不是所有联系人,所以帮我解决这个问题。

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()

        {

          列表与LT; ContactDetails> lst = new List< ContactDetails>();

           lst = GetYahooContacts(" UserId"," Password");&
           

            return View(lst);

        }



       公共列表< ContactDetails> GetYahooContacts(字符串_username,字符串_pass)

        {

           试试
            {

                WebClient webclient = new WebClient();

                webclient.Headers [HttpRequestHeader.UserAgent] = _userAgent;

                webclient.Encoding = Encoding.UTF8;



              &NBSP;&NBSP; byte [] firstResponse = webclient.DownloadData(_loginPage);                

                string firstRes = Encoding.UTF8.GetString(firstResponse);



            &NBSP;&NBSP;&NBSP;&NBSP; NameValueCollection postToLogin = new NameValueCollection();

               正则表达式正则表达式=新正则表达式(" type = \" hidden\" name = \"(。*?)\" value = \"(。*?)\"" ;,RegexOptions.IgnoreCase);
$
               匹配匹配= regex.Match(firstRes);

                while(match.Success)

                {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;如果(match.Groups [0] .Value.Length大于0)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; postToLogin.Add(match.Groups [1]。价值,match.Groups [2]。价值);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;匹配= regex.Match(firstRes,match.Index + match.Length);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; }


                postToLogin.Add(QUOT; .save","登录");

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; postToLogin.Add(" .persistent"," y");
$


          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; string login = _username;

                postToLogin.Add(QUOT;登录" ;,登录);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; postToLogin.Add(" passwd",_ pass);
$


            &NBSP;&NBSP;&NBSP;&NBSP; webclient.Headers [HttpRequestHeader.UserAgent] = _userAgent;

                webclient.Headers [HttpRequestHeader.Referer] = _loginPage;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; webclient.Encoding = Encoding.UTF8;

                webclient.Headers [HttpRequestHeader.Cookie] = webclient.ResponseHeaders [HttpResponseHeader.SetCookie];



         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; webclient.UploadValues(_authUrl,postToLogin);

                

                string cookie = webclient.ResponseHeaders [HttpResponseHeader.SetCookie];
$




        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; string newCookie = string.Empty;

                string [] tmp1 = cookie.Split(',');

               &NBSP; foreach(字符串var在tmp1中)

                {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; string [] tmp2 = var.Split(';');

               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; newCookie = String.IsNullOrEmpty(newCookie)? tmp2 [0]:newCookie +" ;;" + tmp2 [0];

                }


                webclient.Headers [HttpRequestHeader.Cookie] = newCookie;

               字节[] thirdResponse = webclient.DownloadData(_addressBookUrl);&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;  

               串thirdRes = Encoding.UTF8.GetString(thirdResponse);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;列表与LT; ContactDetails> lst = new List< ContactDetails>();

                lst = ParseYahooResponse(thirdRes);

                return lst;

            }
            catch(例外e)

            {

               抛出e;

            }
        }



       私人清单< ContactDetails> ParseYahooResponse(字符串_response)

        {

           列表与LT; ContactDetails> ContactList = new List< ContactDetails>();

            _response = _response.Substring(_response.IndexOf(QUOT; InitialContacts"),(_response.IndexOf(QUOT; InitialBucket") - _response.IndexOf(QUOT; InitialContacts")));



           列表与LT; YahooContacts>触点= JsonConvert.DeserializeObject<列表与LT; 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);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; ocontacts.EmailAddress = Convert.ToString(oYahooContacts.Email);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; ContactList.Add(ocontacts);
$


            }
            return ContactList;

        }
    }

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;
        }
    }

公共类YahooContacts

    {

       私人字符串contactId;

       私人字符串contactName;

       私人字符串电子邮件;





       公共字符串ContactID

        {

           得到{return contactId; }
           设置{contactId = value; }
        }


        public string ContactName

        {

            get {return contactName; }
           设置{contactName = value; }
        }


       公共字符串电子邮件

        {

           得到{return email; }
           设置{email = value; }
        }
    }

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; }
        }
    }

推荐答案

此论坛是用于编程Microsoft项目和Project Server。我建议找一个C#论坛。
This forum is for programming Microsoft Project and Project Server. I suggest to find a C# forum.


这篇关于使用C#导入Yahoo联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆