概述
public class Mail163
{
#region 163邮箱入口定义
const string mail_163_com = "https://reg.163.com/logins.jsp?username={0}&password={1}&type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D35";
/// <summary>
/// 定义公共的CookieHeader变量
/// </summary>
protected static string cookieheader = string.Empty;
/// <summary>
/// 定义下次访问的Url变量
/// </summary>
protected static string nextUrl = string.Empty;
CookieContainer cookieCon = new CookieContainer();
HttpWebRequest req;
HttpWebResponse res;
private string uName;
private string pwd;
private Entrys en;
public Mail163(string name, string pwd, Entrys type)
{
this.uName = name;
this.pwd = pwd;
this.en = type;
}
private string GetHtml()
{
string EntryUrl = GetEntryUrl();
return Process163mail(EntryUrl);
}
private string Process163mail(string entryUrl)
{
try
{
string ReturnHtml = GetRequestHtml(entryUrl, Encoding.GetEncoding("utf-8"));
nextUrl = ReturnHtml.Substring(ReturnHtml.IndexOf("URL=") + 6);
nextUrl = nextUrl.Substring(0, nextUrl.IndexOf("""));
string[] arr = nextUrl.Split(new string []{"&#"},StringSplitOptions.RemoveEmptyEntries);
string str1 = string.Empty;
for (int i = 0; i < arr.Length; i++)
{
int j = int.Parse(arr[i]);
str1 += Encoding.ASCII.GetString(new byte[]{(byte)j});
}
nextUrl = str1;
ReturnHtml = GetRequestHtml(nextUrl, Encoding.GetEncoding("utf-8"));
nextUrl = ReturnHtml.Substring(ReturnHtml.IndexOf("URL=") + 6);
nextUrl = nextUrl.Substring(0,nextUrl.IndexOf("""));
string[] arr1 = nextUrl.Split(new string []{"&#"},StringSplitOptions.RemoveEmptyEntries);
string str2 = string.Empty;
for (int i = 0; i < arr1.Length; i++)
{
int j = int.Parse(arr1[i]);
str2 += Encoding.ASCII.GetString(new byte[] { (byte)j });
}
nextUrl = str2;
ReturnHtml = GetRequestHtml(nextUrl, Encoding.GetEncoding("gb2312"));
nextUrl = res.ResponseUri.AbsoluteUri.Replace("main.jsp?", "address/addrlist.jsp?gid=all&");
ReturnHtml=GetRequestHtml(nextUrl,Encoding.GetEncoding("gb2312"),ReqMethod.GET);
return ReturnHtml;
}
catch(Exception ex)
{
return ex.ToString() + "登录失败,请检查用户名称和密码";
}
}
private string GetRequestHtml(string Url)
{
return GetRequestHtml(Url, Encoding.Default);
}
private string GetRequestHtml(string Url,Encoding encoding)
{
return GetRequestHtml(Url, encoding, ReqMethod.POST);
}
private string GetRequestHtml(string Url, Encoding encoding, ReqMethod rmethod)
{
string html = string.Empty;
try
{
req = (HttpWebRequest)WebRequest.Create(Url);
req.AllowAutoRedirect = true;
req.CookieContainer = cookieCon;
req.Credentials = CredentialCache.DefaultCredentials;
req.Method = rmethod.ToString();
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; Mozilla/4.0(Compatible Mozilla/4.0(Compatible-EmbeddedWB 14.59 http://bsalsa.com/ EmbeddedWB- 14.59 from: http://bsalsa.com/ ; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; InfoPath.2; .NET CLR 3.5.30729; .NET CLR 3.0.30618; CIBA)";
res = (HttpWebResponse)req.GetResponse();
if (cookieheader.Equals(string.Empty))
{
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(Url));
}
else
{
req.CookieContainer.SetCookies(new Uri(Url), cookieheader);
}
html = new StreamReader(res.GetResponseStream(), encoding).ReadToEnd();
}
catch (Exception ex)
{
html = ex.Message;
}
return html;
}
public List<ContactUser> GetContact()
{
List<ContactUser> ls = new List<ContactUser>();
string resHtml = this.GetHtml();
string[] arr = resHtml.Split(new string[] { "查看用户信息">" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 1; i < arr.Length; i = i + 2)
{
ContactUser user = new ContactUser();
user.Name = arr[i].Substring(0, arr[i].IndexOf("<"));
user.Email = arr[i+1].Substring(0, arr[i+1].IndexOf("<"));
ls.Add(user);
}
return ls;
}
private Mail163() { }
private string GetEntryUrl()
{
string EntryUrl = string.Empty;
switch (en)
{
case Entrys.mail163:
EntryUrl = mail_163_com;
break;
default:
break;
}
return string.Format(EntryUrl, uName, pwd);
}
#endregion
}
public enum ReqMethod
{
POST,
GET
}
///枚举,可以添加其它邮箱
public enum Entrys
{
mail163
}
///记录人信息
public class ContactUser
{
private string muserName;
private string memail;
public string Name
{
get { return muserName; }
set { muserName = value; }
}
public string Email
{
get { return memail; }
set { memail = value; }
}
}
转载于:https://www.cnblogs.com/wyux6868/archive/2009/11/11/1601171.html
最后
以上就是灵巧帽子为你收集整理的获取163联系人名字和邮箱地址的全部内容,希望文章能够帮你解决获取163联系人名字和邮箱地址所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复