Net website Automatic Login

Source: Internet
Author: User
Tags getstream

I use it by myself.CodeI wrote a csdn generator and sent it to you for reference. The following describes the specific ideas.

First, we define some variables:
C # code

Private const string net_sessionid = "ASP. net_sessionid = ";
Private const string clientkey = "clientkey = ";

String aspcookie = "";
Private string html = "";
Private string sessionid = "";
Private string clientkey = "";
Private string viewstate = "";

1. Use httpwebrequest to obtain csdn logon information, such as ASP. net_sessionid, clientkey, and _ viewstate. This step can be placed in login_load. The following code is used:
C # code

Private void login_load (Object sender, eventargs E)
{
Httpwebrequest request = webrequest. Create ("http://passport.csdn.net/UserLogin.aspx") as httpwebrequest;
Request. Credentials = credentialcache. defaultcredentials;
Request. Accept = "*/*";
Request. Referer = "http://passport.csdn.net/UserLogin.aspx ";
Request. useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729 ;. net4.0c ;. net4.0e )";
Request. method = "get ";
Request. keepalive = true;
Request. headers. Set ("Accept-language", "ZH-CN ");
Request. headers. Set ("Accept-encoding", "gzip, deflate ");

httpwebresponse response = request. getresponse () as httpwebresponse;
system. io. stream responsestream = response. getresponsestream ();
system. io. streamreader reader = new system. io. streamreader (responsestream, encoding. getencoding ("UTF-8");
html = reader. readtoend ();

String viewstateflag = "id = \" _ viewstate \ "value = \"";
Int I = html. indexof (viewstateflag) + viewstateflag. length;
Int J = html. indexof ("\" ", I );
Viewstate = html. substring (I, j-I );

Aspcookie = response. headers. Get ("Set-cookie ");
Sessionid = getsessionid ();
Clientkey = getclientkey ();
// Obtain the image Verification Code
Getcode ();
}
/// <Summary>
/// Obtain the clientkey
/// </Summary>
/// <Returns> </returns>
Private string getclientkey ()
{
// This is only used to obtain the length of the clientkey value.
String id = "bb32434c-3bb3-4e44-92c3-8952f631ca87 ";
Int Index = aspcookie. indexof (clientkey) + clientkey. length;
String STR = aspcookie. substring (index, id. Length );
Return STR;
}

/// <Summary>
/// Obtain ASP. net_sessionid =
/// </Summary>
/// <Returns> </returns>
Private string getsessionid ()
{
String id = "5mhl0tvbw5shlpnhxgwnck45 ";
Int Index = aspcookie. indexof (net_sessionid) + net_sessionid.length;
String STR = aspcookie. substring (index, id. Length );
Return STR;
}

2. Obtain the image Verification Code. In this step, note that we use another httpwebrequest to obtain the verification code. To ensure that the obtained verification code is consistent with the session on the logon page, we need to put session and other information into the header. The following code:
C # code

/// <Summary>
/// Generate a new verification code
/// </Summary>
Private void getcode ()
{
Try
{
Cookiecollection cookies = new cookiecollection ();
Cookies. Add (new cookie ("ASP. net_sessionid", sessionid ));
Cookies. Add (new cookie ("clientkey", clientkey ));

Httpwebrequest;
Httpwebresponse webresponse;
Byte [] byterequest = {};
Httpwebrequest = (httpwebrequest) httpwebrequest. Create ("http://passport.csdn.net/ShowExPwd.aspx ");
Cookiecontainer CO = new cookiecontainer ();
CO. Add (New uri ("http://passport.csdn.net"), cookies );
Httpwebrequest. cookiecontainer = Co;
Httpwebrequest. Accept = "*/*";
Httpwebrequest. Referer = "http://passport.csdn.net/UserLogin.aspx ";
Httpwebrequest. useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729 ;. net4.0c ;. net4.0e )";
Httpwebrequest. method = "get ";
Httpwebrequest. headers. Set ("Accept-encoding", "gzip, deflate ");
Httpwebrequest. headers. Set ("Accept-language", "ZH-CN ");
Httpwebrequest. keepalive = true;

Webresponse = (httpwebresponse) httpwebrequest. getresponse ();
Stream responsestream = webresponse. getresponsestream ();
Image original = image. fromstream (responsestream );
Bitmap bitmap = new Bitmap (original );
This. picturebox1.image = bitmap;
Responsestream. Close ();
}
Catch (exception)
{
MessageBox. Show ("error:" + exception. Message );
}
}

3. The last step is the logon event. It is easy to log on. Just put the relevant information into the request data.
C # code

Private void btnlogin_click (Object sender, eventargs E)
{
Httpwebrequest;
Httpwebresponse webresponse;

string randnum = txtrandnum. Text;
string Password = txtpassword. Text;
string loginname = txtusername. Text;

string postdata = "ctl00 $ cph_content $ response = {0} & ctl00 $ cph_content $ tb_password = {1} & ctl00 $ cph_content $ tb_expwd = {2} & __ eventtarget = & __ eventargument = & __ viewstate = {3} & clientkey = {4} & from = {5} & mailparameters = & prepage = & ctl00 $ cph_content $ image_login.x = 46 & ctl00 $ cph_content $ image_login.y = 9 ";
postdata = string. format (postdata, system. web. httputility. urlencode (loginname), system. web. httputility. urlencode (password), system. web. httputility. urlencode (randnum), system. web. httputility. urlencode (viewstate), clientkey, system. web. httputility. urlencode ("http://hi.csdn.net/");
byte [] byterequest = encoding. utf8.getbytes (postdata);

Cookiecollection cookies = new cookiecollection ();
Cookies. Add (new cookie ("ASP. net_sessionid", sessionid ));
Cookies. Add (new cookie ("clientkey", clientkey ));
Cookies. Add (new cookie ("UN", loginname ));

Httpwebrequest = (httpwebrequest) httpwebrequest. Create ("http://passport.csdn.net/UserLogin.aspx ");
Cookiecontainer CO = new cookiecontainer ();
CO. Add (New uri ("http://passport.csdn.net"), cookies );
Httpwebrequest. cookiecontainer = Co;
Httpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";
Httpwebrequest. accept = "image/JPEG, application/X-MS-application, image/GIF, application/XAML + XML, image/pjpeg, application/X-MS-xbap, application/X-Shockwave-flash ,*/*";
Httpwebrequest. Referer = "http://passport.csdn.net/UserLogin.aspx ";
Httpwebrequest. useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729 ;. net4.0c ;. net4.0e )";
Httpwebrequest. method = "Post ";
Httpwebrequest. keepalive = true;
Httpwebrequest. headers. Set ("cache-control", "No-Cache ");
Httpwebrequest. headers. Set ("Accept-encoding", "gzip, deflate ");
Httpwebrequest. headers. Set ("Accept-language", "ZH-CN ");

httpwebrequest. contentlength = byterequest. length;
stream = httpwebrequest. getrequeststream ();
stream. write (byterequest, 0, byterequest. length);
stream. close ();
webresponse = (httpwebresponse) httpwebrequest. getresponse ();
system. io. stream responsestream = webresponse. getresponsestream ();

String header = webresponse. headers. tostring ();
Stream getstream = webresponse. getresponsestream ();
Long contentlength = webresponse. contentlength;

Byte [] outbytes = new byte [contentlength];
Outbytes = readfully (getstream );
Getstream. Close ();

Getstream = new memorystream (outbytes );
Streamreader = new streamreader (getstream, encoding. utf8 );
String getstring = streamreader. readtoend ();
Streamreader. Close ();
Getstream. Close ();

}

Static byte [] readfully (Stream)
{
Byte [] buffer = new byte [128];
Using (memorystream MS = new memorystream ())
{
While (true)
{
Int READ = stream. Read (buffer, 0, buffer. Length );
If (read <= 0)
Return Ms. toarray ();
Ms. Write (buffer, 0, read );
}
}
}

It took a lot of time to post the data and the data to the server. Later, httpanalyzer was used to capture the login data packets in the browser and compare them with the winform login data packets, to locate the problem.

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.