在asp.net中导入yahoo联系人时获取垃圾值...? [英] Getting garbage value while importing yahoo contacts in asp.net...?
问题描述
我在asp.net应用程序中导入了yahoo联系人。
但是在导入雅虎联系人的过程中,我觉得这样;
sans-serif; * font-size:small; * font:x-small ;}
我怎么能解决它?
我的代码是:
Yahoo!xtract.cs中的
公共类YahooExtract:IMailContactExtract
{
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;
字符串名称;
字符串电子邮件;
// #region IMailContactExtract会员
公共字符串Extract(字符串用户名,字符串密码)
{
bool result = false;
// list = new MailContactList();
尝试
{
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();
正则表达式正则表达式=新正则表达式(type = \hidden \name = \(。*?)\value = \(。*?)\,RegexOpt ions.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,登录);
postToLogin.Add(.persistent,y);
// string login = credential.UserName.Split(''@'')[0];
// @ postToLogin.Add(login ,登录);
// @ postToLogin.Add(passwd,credential.Password);
postToLogin.Add(login ,用户名);
postToLogin.Add(passwd,密码);
webClient.Headers [HttpRequestHeader.UserAgent] = _userAgent;
webClient.Headers [ HttpRequestHeader.Referer] = _loginPage;
webClient.Encoding = Encoding.UTF8;
webClient.Headers [HttpRequestHeader.Cookie] = webClient.ResponseHeaders [HttpResponseHeader.SetCookie]; < br $>
webClient.UploadValues(_authUrl,postToLogin);
string cookie = webClient.ResponseHeaders [HttpResponseHeader.SetCookie];
if(string.IsNullOrEmpty(cookie))
{
//返回false;
}
string 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);
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],立即导出);
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 [] items = line.Split('','');
if(items.Length< 5)
{
继续;
}
email = items [4];
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
{
}
//返回列表;
//返回名称;
返回电子邮件;
}
}
//在IMailContactExtract.cs中
公共接口IMailContactExtract
{
string Extract(string username,string password );
}
//在MailContact.cs中
公共类MailContact
{
private string _email = string.Empty;
private string _name = string.Empty;
公共字符串名称
{
get {return _name; }
set {_name = value; }
}
公共字符串电子邮件
{
get {return _email ; }
set {_email = value; }
}
}
//在Default.aspx.cs中
protected void LnkBtnAtGetContacts_Click(object sender,EventArgs e)
{
// MailContactList list = null;
YahooExtract extractor = new YahooExtract();
// bool res = extractor.Extract(new NetworkCredential(TxtATUsername.Text.ToString(),TxtATPassword.Text.ToString()),out list);
//Console.WriteLine(res?" Succeeded":" Failed");
string email = extractor.Extract(TxtATUsername.Text.ToString(), TxtATPassword.Text.ToString());
Response.Write(email);
}
I have imported yahoo contacts in my asp.net application.
But while importing yahoo contacts i get like;
sans-serif;*font-size:small;*font:x-small;}
How could i solve it?
My code is:
in YahooExtract.cs
public class YahooExtract: IMailContactExtract
{
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";
string name;
string email;
//#region IMailContactExtract Members
public string Extract(string username, string password)
{
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 );
postToLogin.Add("login", username);
postToLogin.Add( "passwd", 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;
}
email = items[ 4 ];
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;
//return result;
}
catch
{
}
//return list;
//return name;
return email;
}
}
//In IMailContactExtract.cs
public interface IMailContactExtract
{
string Extract(string username, string password);
}
//In MailContact.cs
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; }
}
}
//In Default.aspx.cs
protected void LnkBtnAtGetContacts_Click(object sender, EventArgs e)
{
//MailContactList list = null;
YahooExtract extractor = new YahooExtract();
// bool res = extractor.Extract(new NetworkCredential(TxtATUsername.Text.ToString(),TxtATPassword.Text.ToString()), out list);
//Console.WriteLine(res ? "Succeeded" : "Failed");
string email= extractor.Extract(TxtATUsername.Text.ToString(),TxtATPassword.Text.ToString());
Response.Write(email);
}
推荐答案
我的猜测是你没有从雅虎正确导出联系人。您应该将联系人称为CSV(逗号分隔)。
My guess is that you didn''t export the contact properly from Yahoo. You should expert the contacts as CSV (comma separated).
这篇关于在asp.net中导入yahoo联系人时获取垃圾值...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!