Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Text; Using System.Net; Using System.IO; Using System.Xml; Namespace Getemailaddress
{
public class Address163
{
Mailbox Entry Definition
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%3d-1 "; Determine if the username and password are empty/correct
protected static string Cookieheader = String. Empty; Defines a public Cookie Header variable
protected static string Nexturl = String. Empty; Define URL variables for next access
Cookiecontainer Cookiecon = new Cookiecontainer ();
HttpWebRequest req;
HttpWebResponse Res;
private string UName;
private string pwd;
Private Entrys en;
<summary>
Constructors
</summary>
<param name= "name" > Mailbox name </param>
<param name= "pwd" > Mailbox password </param>
<param name= "Type" ></param>
Public Address163 (String name,string pwd,entrys type)
{
This.uname = name;
This.pwd = pwd;
En=type;
}
<summary>
Get Web page data
</summary>
<returns> get HTML data for Web pages </returns>
private String gethtml ()
{
String entryurl = Getentryurl ();
Return Process163mail (Entryurl);
}
<summary>
Analysis 163
</summary>
<param name= "Entryurl" > Resolution address </param>
<returns></returns>
private string Process163mail (String entryurl)
{
Try
{
#1
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});//str1
}
Nexturl = str1;
Ignore second request address
Nexturl = str2;
#3
Nexturl = "Http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1 &style=35&username= "+uname.replace" ("@163.com", "");
returnhtml = getrequesthtml (Nexturl, encoding.getencoding ("gb2312"));
Redirect to Http://cg1a181.mail.163.com/js3/main.jsp?sid=eELXghnSsySYEzpNbLSSrMNUaUSOCRib caused by server
String post = "<?xml version=" 1.0 "><object><array name=" items "><object><string name=" Func ">pab:searchcontacts</string><object name=" var "><array name=" order "><object>< String name= "field" >fn</string><boolean name= "IgnoreCase" >true</boolean></object>< /array></object></object><object><string name= "func" >user:getsignatures</string ></object><object><string name= "func" >pab:getallgroups</string></object></ Array></object> ";
byte[] PB = Encoding.ASCII.GetBytes (post. ToString ()); Nexturl = Res. Responseuri.absoluteuri; String Bookurl = (Nexturl + "&func=global:sequential"). Replace ("Js3", "a");
Req = (HttpWebRequest) httpwebrequest.create (new Uri (Bookurl). Replace ("main.jsp", "s"));
Req. method = "POST";
Req. ContentType = "Application/xml";
Req. ContentLength = PB. Length;
Req. useragent = "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.2; SV1; GTB6. NET CLR 1.1.4322;. NET CLR 2.0.50727;. NET CLR 3.0.04506.648;. NET CLR 3.5.21022) ";
Req. Cookiecontainer = Cookiecon; Send data using (Stream s = req. GetRequestStream ()) { S.write (Pb, 0, PB. Length); } Get return information
using (HttpWebResponse WR = (HttpWebResponse) req. GetResponse ())
{
StreamReader sr = new StreamReader (WR. GetResponseStream (), Encoding.UTF8);
returnhtml = Sr. ReadToEnd ();
Sr. Close ();
}
return returnhtml;
}
catch (Exception ex)
{
Return ex. ToString () + "Login failed, please check user name and password";
}
} <summary>
Submit to Server
</summary>
<param name= "URL" > url</param> to submit
<returns> Return HTML content </returns>
private string getrequesthtml (String URL)
{
Return getrequesthtml (URL, Encoding.default);
}
private string getrequesthtml (String URL, Encoding encodeing)
{
Return getrequesthtml (url,encodeing, reqmethod.post);//encoding.default
}
private string getrequesthtml (String URL, Encoding encodeing,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 (), encodeing). ReadToEnd (); } catch (Exception ex) { html = ex. message; } return HTML; } <summary>
Get the contents of the 163 address Book
</summary>
<returns> Address Book Collection </returns>
Public list<person> getcontact ()
{
list<person> ls = new list<person> ();
Read the XML data and make a selection match filter out the matching mailboxes
String reshtml = Encoding.UTF8.GetString (Encoding.convert (Encoding.UTF8, Encoding.UTF8, Encoding.UTF8.GetBytes (this . Gethtml ()));
XmlDocument xmldoc = new XmlDocument ();
if (!string. IsNullOrEmpty (reshtml))
{
Xmldoc.loadxml (reshtml);
}
XmlNodeList XNL = xmldoc.selectnodes ("/result/array/object");
if (xnl = null | | xnl. Count <= 0)
return LS; XmlNodeList Linknodes = xnl[0]. SelectNodes ("Array/object");
foreach (XmlNode linknode in Linknodes)
{
person PS = new person ();
foreach (XmlNode xn2 in Linknode.childnodes)
{
Get email address
if (xn2. attributes["Name"]. Value = = "EMAIL; Pref ")
{
Ps. Email = xn2. InnerText;
}
if (xn2. attributes["Name"]. Value = = "FN")
{
if (!string. IsNullOrEmpty (xn2. innertext))
{
Ps. Name = xn2. InnerText;
}
Else
{
Ps. name = "No Name temporarily";
}
}
}
Ls. ADD (PS);
}
return LS;
}
<summary>
Enumeration how the request was requested
</summary>
Private Enum Reqmethod
{
POST,
Get
}
<summary>
Enumeration gets the entry address
</summary>
public enum Entrys {
mail163,
mail126,
MAILQQ,
Mailyeah
}
<summary>
Get the request address
</summary>
<returns> GET request Address </returns>
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);
}
Private Address163 () {}//enclosing interface } } |