在Web应用程序中导入yahoo联系人(yahoo API) [英] To import yahoo contacts in the web application (yahoo API)

查看:82
本文介绍了在Web应用程序中导入yahoo联系人(yahoo API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在Web应用程序中导入Yahoo联系人..

但它不起作用..

可以对此代码进行任何更改?



I am using this code to import yahoo contacts in a web application..

but its not working..

any changes that can be made to this code??



using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Gnilly.Syndication.Mail
{
    public class YahooExtract
    {
        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 bool Extract( NetworkCredential credential, out MailContactList list )
        {
            bool result = false;

            list = new MailContactList();

            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 = credential.UserName.Split( ''@'' )[ 0 ];
                postToLogin.Add( "login", login );
                postToLogin.Add( "passwd", credential.Password );

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

                if ( string.IsNullOrEmpty( cookie ) )
                {
                    return false;
                }

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

                // set login cookie
                webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
                byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
                string thirdRes = Encoding.UTF8.GetString( thirdResponse );

                string crumb = string.Empty;
                Regex 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;
                }


                NameValueCollection postDataAB = new NameValueCollection();
                postDataAB.Add( ".crumb", crumb );
                postDataAB.Add( "vcp", "import_export" );
                postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );

                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
                webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;

                byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
                string csvData = Encoding.UTF8.GetString( FourResponse );

                string[] 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;
                            list.Add( mailContact );
                        }
                    }
                }

                result = true;
            }
            catch
            {
            }
            return result;
        }
    }

    public class MailContact
    {
       private string _email = string.Empty;
       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; }
       }
    }

    public class MailContactList : List<MailContact>
    {
    } 

}






感谢和拉加德...






Thanks and Ragards...

推荐答案

看这里:
Yahoo!在C#中联系导入类 [ ^ ]
使用asp.net和c#从yahoo获取联系人 [
Look here:
Yahoo! Contact import class in C#[^]
Fetch Contacts From yahoo using asp.net and c#[^]


您已经问过这个问题.再重复一次完全相同的问题是没有意义的-实际上,它使人们感到烦恼,因此您不太可能获得有用的答案.

另外,请问您输入问题的屏幕上的第4项,内容为:-

4.使问题尽可能简洁.如果您必须包含代码,请包含最小的代码片段-不要转储整个代码库.


期望繁忙的人们在您所说的所有代码不起作用时会经历所有这些代码,这是很多事情–它以什么方式不起作用".如果您不能将问题缩小到更小的代码部分,则至少要更详细地描述问题(最好同时执行).

如果可以的话,我相信有人会为您提供有关此问题的帮助. :thumbsup:

祝您好运:)
You have already asked this question. There is no point in repeating the exact same question again - in fact it annoys people so you are less likely to get a usefull answer.

Also, may I refer you to item 4 on the screen where you entered your questions, it says:-

4. Keep the question as concise as possible. If you have to include code, include the smallest snippet of code you can - do not dump your entire codebase.


It is a lot to expect busy people to go through all this code when all you say is it is not working - in what way is it ''not working''. If you cannot narrow the problem down to a smaller peice of code at least describe the problem in greater detail (preferaby do both).

If you can do that I am sure someone will be able to give you some help with this problem. :thumbsup:

Best of Luck :)


使用系统;
使用System.Collections.Specialized;
使用System.Net;
使用System.Text;
使用System.Text.RegularExpressions;

命名空间Gnilly.Syndication.Mail
{
公共类YahooExtract
{
私有常量字符串_addressBookUrl ="http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&amp;.rand=1671497644&A=H&amp;Yahoo_ab.csv";
私有常量字符串_authUrl ="https://login.yahoo.com/config/login?";
私有常量字符串_loginPage ="https://login.yahoo.com/config/login";
private const string _userAgent ="Mozilla/5.0(Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.3)Gecko/20070309 Firefox/2.0.0.3";

公共布尔摘录(NetworkCredential凭据,出MailContactList列表)
{
bool结果=假;

list = new MailContactList();

试试
{
WebClient webClient =新的WebClient();
webClient.Headers [HttpRequestHeader.UserAgent] = _userAgent;
webClient.Encoding = Encoding.UTF8;

byte [] firstResponse = webClient.DownloadData(_loginPage);
字符串firstRes = Encoding.UTF8.GetString(firstResponse);


NameValueCollection postToLogin =新的NameValueCollection();
Regex regex = new Regex("type = \" hidden \"name = \"(.*?)\"value = \"(.*?)\",RegexOptions.IgnoreCase);
匹配match = regex.Match(firstRes);
while(match.Success)
{
if(match.Groups [0] .Value.Length& gt; 0)
{
postToLogin.Add(match.Groups [1] .Value,match.Groups [2] .Value);
}
match = regex.Match(firstRes,match.Index + match.Length);
}


postToLogin.Add(".save",登录");
postToLogin.Add(".persistent","y");

字符串登录= credential.UserName.Split(``@'')[0];
postToLogin.Add("login",login);
postToLogin.Add("passwd",credential.Password);

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);
字符串cookie = webClient.ResponseHeaders [HttpResponseHeader.SetCookie];

if(string.IsNullOrEmpty(cookie))
{
返回false;
}

字符串newCookie = string.Empty;
string [] tmp1 = cookie.Split('','');
foreach(tmp1中的字符串var)
{
string [] tmp2 = var.Split(``;'');
newCookie = String.IsNullOrEmpty(newCookie)吗? tmp2 [0]:newCookie +;" + tmp2 [0];
}

//设置登录cookie
webClient.Headers [HttpRequestHeader.Cookie] = newCookie;
byte [] thirdResponse = webClient.DownloadData(_addressBookUrl);
字符串thirdRes = Encoding.UTF8.GetString(thirdResponse);

字符串屑= string.Empty;
Regex regexCrumb =新的Regex("type = \" hidden \"name = \" \\.crumb \"id = \" crumb1 \"value = \"(.*?)\",RegexOptions.IgnoreCase);
匹配= regexCrumb.Match(thirdRes);
if(match.Success& amp; amp; match.Groups [0] .Value.Length& gt; 0)
{
crumb = match.Groups [1] .Value;
}


NameValueCollection postDataAB =新的NameValueCollection();
postDataAB.Add(".crumb",crumb);
postDataAB.Add("vcp","import_export");
postDataAB.Add("submit [action_export_yahoo]",立即导出");

webClient.Headers [HttpRequestHeader.UserAgent] = _userAgent;
webClient.Headers [HttpRequestHeader.Referer] = _addressBookUrl;

byte [] FourResponse = webClient.UploadValues(_addressBookUrl,postDataAB);
字符串csvData = Encoding.UTF8.GetString(FourResponse);

string [] lines = csvData.Split(``\ n'');
foreach(行中的字符串行)
{
string []项目= line.Split('','');
if(items.Length& lt; 5)
{
继续;
}
字符串电子邮件=项目[4];
字符串名称=项目[3];
if(!string.IsNullOrEmpty(email)& amp; amp;!string.IsNullOrEmpty(name))
{
email = email.Trim(``\'');
name = name.Trim(``\'');
if(!email.Equals("Email")& amp!amp;!name.Equals("Nickname"))
{
MailContact mailContact =新的MailContact();
mailContact.Name =名称;
mailContact.Email =电子邮件;
list.Add(mailContact);
}
}
}

结果= true;
}
赶上
{
}
返回结果;
}
}

公共类MailContact
{
私有字符串_email = string.Empty;
私有字符串_name = string.Empty;

公共字符串名称
{
得到{return _name; }
设置{_name = value; }
}

公用字符串电子邮件
{
得到{return _email; }
设置{_email = value; }
}

公共字符串FullEmail
{
得到{返回电子邮件; }
}
}

公共类MailContactList:List& lt; MailContact& gt;
{
}

} ARUN PATIDAR SARVATMAN技术
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Gnilly.Syndication.Mail
{
public class YahooExtract
{
private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&amp;.rand=1671497644&amp;A=H&amp;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 bool Extract( NetworkCredential credential, out MailContactList list )
{
bool result = false;

list = new MailContactList();

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 &gt; 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 = credential.UserName.Split( ''@'' )[ 0 ];
postToLogin.Add( "login", login );
postToLogin.Add( "passwd", credential.Password );

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

if ( string.IsNullOrEmpty( cookie ) )
{
return false;
}

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

// set login cookie
webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
string thirdRes = Encoding.UTF8.GetString( thirdResponse );

string crumb = string.Empty;
Regex regexCrumb = new Regex( "type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
match = regexCrumb.Match( thirdRes );
if ( match.Success &amp;&amp; match.Groups[ 0 ].Value.Length &gt; 0 )
{
crumb = match.Groups[ 1 ].Value;
}


NameValueCollection postDataAB = new NameValueCollection();
postDataAB.Add( ".crumb", crumb );
postDataAB.Add( "vcp", "import_export" );
postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );

webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;

byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
string csvData = Encoding.UTF8.GetString( FourResponse );

string[] lines = csvData.Split( ''\n'' );
foreach ( string line in lines )
{
string[] items = line.Split( '','' );
if ( items.Length &lt; 5 )
{
continue;
}
string email = items[ 4 ];
string name = items[ 3 ];
if ( !string.IsNullOrEmpty( email ) &amp;&amp; !string.IsNullOrEmpty( name ) )
{
email = email.Trim( ''\"'' );
name = name.Trim( ''\"'' );
if ( !email.Equals( "Email" ) &amp;&amp; !name.Equals( "Nickname" ) )
{
MailContact mailContact = new MailContact();
mailContact.Name = name;
mailContact.Email = email;
list.Add( mailContact );
}
}
}

result = true;
}
catch
{
}
return result;
}
}

public class MailContact
{
private string _email = string.Empty;
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; }
}
}

public class MailContactList : List&lt;MailContact&gt;
{
}

}ARUN PATIDAR SARVATMAN TECHNOLOGIES


这篇关于在Web应用程序中导入yahoo联系人(yahoo API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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