Use C # To automatically verify logon during collection

Source: Internet
Author: User
Recently, a friend asked me how to collect information on some websites with user or password verification. Recently, this was used in the project for verification login, I will share some of the Code with you. This is just a prototype trial I made on the Internet. Although it can be used, the performance may not be so good .. Currently, it is only valid for websites that do not have a logon verification code. You have to write an analytical verification class for get .....

Recently, my work has been messy and I haven't had time to sort it out. These are all temporarily used, and I 've been busy learning Ubuntu recently. net + MySQL project. There is also interstellar.

First open the website, view the source file, and find its logon form.

For example:
<Form name = "login" Action = "loginmain. jsp" method = "Post" target = "_ top">
<Table width = "218" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<TD width = "50" Height = "28" class = "Hui"> <Div align = "right"> Username: </div> </TD>
<TD width = "168" Height = "28"> <input class = "Hui" id = "username"
Maxlength = "40" size = "23" name = "username" id = "username"/> </TD>
</Tr>
<Tr>
<TD Height = "28" class = "Hui"> <Div align = "right"> password: </div> </TD>
<TD Height = "28"> <input class = "Hui" id = "passwd"
Maxlength = "40" size = "23" name = "passwd" type = "password" id = "passwd"/> </TD>
</Tr>
</Table>
</Form>
From the above form, we can see that the form submission method is: Post, submitted to loginmain. jsp for processing, there are two form items: username, passwd

Below is the C # imitation login program: Login. CS

Using system;
Using system. Data;
Using system. net;
Using system. text;
Using system. IO;
Using system. Text. regularexpressions;

/** // <Summary>
/// Log on to the website and obtain cookies
/// </Summary>
/// <Returns> Successful Logon Cookie Information </returns>
Public static cookiecontainer get_login ()
...{
Cookiecontainer cc = new cookiecontainer ();
String formurl = "http://blog.hnce.net/loginMain.jsp"; // process the form's absolute URL address
String formdata = "username = slick & passwd = hedy12345"; // parameters to be submitted in the form. Note that they are changed to the information you have registered.
Asciiencoding encoding = new asciiencoding ();
Byte [] DATA = encoding. getbytes (formdata );

Httpwebrequest request = (httpwebrequest) webrequest. Create (formurl );
Request. method = "Post"; // data submission method
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. contentlength = data. length;
Request. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1;. Net CLR 1.1.4322 )";
// Simulate a useragent
Stream newstream = request. getrequeststream ();
Newstream. Write (data, 0, Data. Length );

Newstream. Close ();

Request. cookiecontainer = cc;

Httpwebresponse response = (httpwebresponse) request. getresponse ();
Cc. Add (response. Cookies );
Stream stream = response. getresponsestream ();
String webcontent = new streamreader (stream, system. Text. encoding. Default). readtoend ();
Return cc;
}

Call the preceding method to obtain information that requires logon.

Cookiecontainer cc = new cookiecontainer ();
Cc = login. get_login (); // get logon cookies

String photoclassurl = "http://blog.hnce.net/xxx.jsp ";
Httpwebrequest myrequest = (httpwebrequest) webrequest. Create (photoclassurl );
Myrequest. cookiecontainer = cc;
Httpwebresponse myresponse = (httpwebresponse) myrequest. getresponse ();
Cc. Add (myresponse. Cookies );
Stream mystream = myresponse. getresponsestream ();
String shtml = new streamreader (mystream, system. Text. encoding. Default). readtoend ();

Shtml is what you see after you log on. The program write is messy. If you have any comments or are unclear, you can send me an email: hedongyang # gmail.com
(# Change)
 

The above is transferred from: http://blog.csdn.net/hedongyang/archive/2007/05/15/1609379.aspx

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.