asp.net crawl 126 mailbox Contact code

Source: Internet
Author: User
Tags foreach
The code is as follows Copy Code

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Text;
Using System.Net;
Using System.IO;
Using System.Text.RegularExpressions;
Using System.Xml;

Namespace Getemailaddress
{
public class Address126
{
Mailbox Entry Definition
Const string mail126 = "http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/ Cgi/ntesdoor?hid%3d10010102%26lightweight%3d1%26verifycookie%3d1%26language%3d0%26style%3d-1 ";
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 ();

private string UName;


private string pwd;


Private GetEmailAddress.Address163.Entrys en;


Regex reg;


Match m;


Byte[] B;


<summary>


Constructors


</summary>


<param name= "name" > Mailbox name </param>


<param name= "pwd" > Mailbox password </param>


<param name= "Type" ></param>


Public Address126 (string name, string pwd, GetEmailAddress.Address163.Entrys type)


{


This.uname = name;


This.pwd = pwd;


En = type;


Log the username and password of the login mailbox


StringBuilder sb = new StringBuilder ();


Sb. Append ("&domain=126.com");//Field


Sb. Append ("&language=-1");//language


Sb. Append ("&bcookie=");


Sb. Append ("&username=" + name);


Sb. Append ("&savelogin=");


Sb. Append ("url2=http://mail.126.com/errorpage/err_126.htm");


Sb. Append ("&user=" + name.) Substring (0,name. IndexOf ("@"));//Login name


Sb. Append ("&password=" + pwd);//password


Sb. Append ("&style=-1"); Style


Sb. Append ("&secure=");


b = Encoding.ASCII.GetBytes (sb.) ToString ());


}


<summary>


Get Web page data


</summary>


<returns> get HTML data for Web pages </returns>


private String gethtml ()


{


String entryurl = Getentryurl ();


Return Process126mail (Entryurl);


}


<summary>


Analysis 126


</summary>


<param name= "Entryurl" > Resolution address </param>


<returns></returns>


private string Process126mail (String entryurl)


{


Try


{


#region//First request for landing address


First request 126 Login email Address "http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ Ntesdoor?hid%3d10010102%26lightweight%3d1%26verifycookie%3d1%26language%3d0%26style%3d-1 ";


Get the content of the request


HttpWebRequest HWR = (HttpWebRequest) httpwebrequest.create (new Uri (Entryurl));


Hwr. method = "POST";


Hwr. KeepAlive = false;


Hwr. ContentType = "application/x-www-form-urlencoded";


Hwr. ContentLength = B.length;


Hwr. 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) ";


Hwr. Cookiecontainer = Cookiecon;


               ////Send data
                using (Stream s = hwr. GetRequestStream ())
                 {
                     S.write (b, 0, b.length);
               }
                String rb = String. Empty;

Get return information
using (HttpWebResponse WR = (HttpWebResponse) hwr. GetResponse ())
{

StreamReader sr = new StreamReader (WR. GetResponseStream (), Encoding.UTF8);
RB = Sr. ReadToEnd ();
Sr. Close ();
}

reg = new Regex (@ "Http-equiv=refresh content=" "[d]; Url= (. +?) "" > ");
m = Reg. Match (RB. ToUpper ());
if (m.success)
{
Entryurl = m.groups[1]. Value.tolower ();
}

Entryurl = Transcode (Entryurl);

Add username and password to head cookies


Uri nurl1 = new Uri ("http://www.163.com");


Uri nurl2 = new Uri ("http://reg.163.com");


Uri nurl3 = new Uri (Entryurl);


foreach (System.Net.Cookie Cookie in Cookiecon.getcookies (NURL1))


{


Cookie. Domain = Nurl3. Host;


}


Cookiecon.add (Cookiecon.getcookies (NURL1));

                foreach ( System.Net.Cookie Cookie in Cookiecon.getcookies (NURL2))
                 {
                     cookies. Domain = Nurl3. Host;
               }
                Cookiecon.add ( Cookiecon.getcookies (NURL2));

                foreach ( System.Net.Cookie Cookie in Cookiecon.getcookies (NURL3))
                 {
                     cookies. Domain = ". 126.com";
                     cookies. Expires = DateTime.Now.AddHours (1);
               }
                Cookiecon.add ( Cookiecon.getcookies (NURL3));
                #endregion

Ignore second request new address





#region//Third request for a new address


Get a third address based on the second address





String rurl = String. Empty;


String threrrurl = "Http://entry.mail.126.com/cgi/ntesdoor?username=" + uName + "&hid=10010102&lightweight=1 &verifycookie=1&language=0&style=-1 ";


HWR = (HttpWebRequest) httpwebrequest.create (new Uri (Threrrurl));


Hwr. 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) ";


Hwr. Cookiecontainer = Cookiecon;


Hwr. method = ' head ';


Hwr. AllowAutoRedirect = false;

               //Get return information
                using ( HttpWebResponse WR = (HttpWebResponse) hwr. GetResponse ())
                {
                     Rurl = wr. getResponseHeader ("Location");  //Get to Rurl http://cwebmail.mail.126.com/js4/main.jsp?sid= WBGSOZLBKEKZAFRFIVBBQBKXSHBOWIPX
                }
                 #endregion

#region////for the fourth time to request a new address for data
Access to http://cwebmail.mail.126.com/js4/s?sid=WBGSoZlBkEKZafrfivBBqbkxsHBowIPx&func=global:sequential based on the third address &from=nav&action=showhidefolder&showad=false&usertype=newuser&uid=xxxx@sina.com

Then request the parse to get the contact person to save as XML data


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= "desc" >false</boolean><boolean name= " IgnoreCase ">true</boolean></object></array></object></object><object> <string name= "func" >pab:getAllGroups</string></object></array></object>;


byte[] PB = Encoding.ASCII.GetBytes (post. ToString ());


String Bookurl = (Rurl + "&func=global:sequential"). Replace ("Js3", "a");


HWR = (HttpWebRequest) httpwebrequest.create (new Uri (Bookurl). Replace ("main.jsp", "s"));


Hwr. method = "POST";


Hwr. ContentType = "Application/xml";


Hwr. ContentLength = PB. Length;


Hwr. 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) ";


Hwr. Cookiecontainer = Cookiecon;

Send data
using (Stream s = hwr. GetRequestStream ())
{
S.write (Pb, 0, PB. Length);
}

Get return information


using (HttpWebResponse WR = (HttpWebResponse) hwr. GetResponse ())


{


StreamReader sr = new StreamReader (WR. GetResponseStream (), Encoding.UTF8);


RB = Sr. ReadToEnd ();


Sr. Close ();


}


#endregion

                return RB;
           }
            catch (Exception ex)
             {
                 return ex. ToString () + "Login failed, check user name and password";

}
}
Regular filter
private static string Transcode (String str)
{
Regex r = new Regex (@ "&# ([d]{1,5})", Regexoptions.none);
StringBuilder s = new StringBuilder ();

foreach (Match m in R.matches (str))


{


char C = (char) int. Parse (M.groups[1]. Value);


S.append (C.tostring ());


}


return s.tostring ();


}


<summary>


Get the contents of the 126 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 ();


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;


}


&lt;summary&gt;


Enumeration how the request was requested


&lt;/summary&gt;


Private Enum Reqmethod


{


POST,


Get


}


&lt;summary&gt;


Get the request address


&lt;/summary&gt;


&lt;returns&gt; GET request Address &lt;/returns&gt;


private String Getentryurl ()


{


String Entryurl = String. Empty;


Switch (en)


{


Case GETEMAILADDRESS.ADDRESS163.ENTRYS.MAIL126:


Entryurl = mail126;


Break


Default


Break


}


return string. Format (Entryurl, UName, PWD);


}


Private Address126 ()


{


}//Closed interface

   }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.