C # remotely log on to a website with a verification code

Source: Internet
Author: User

C # remotely log on to a website with a verification code
Step 1: Add a space reference

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. net;
Using system. IO;
Using system. text;
Using system. diagnostics;
Using system. Web;

Step 2: click the button to run the code

Private void button4_click (Object sender, system. eventargs E)
{
This. button4.enabled = false;
// Step 1: Obtain the system login verification code
String url = "http: // localhost: 8001/login. asp ";
String indata = "";
String outdata = "";
Cookiecontainer mycookiecontainer = new cookiecontainer ();
// Create a cookiecontainer to store the cookie set
Httpwebrequest myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
// Create an httpwebrequest
Myhttpwebrequest. Referer = "http: // localhost: 8001/login. asp ";
Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";
Myhttpwebrequest. contentlength = indata. length;
Myhttpwebrequest. method = "Post ";
Myhttpwebrequest. cookiecontainer = mycookiecontainer;
// Set cookiecontainer of httpwebrequest to the mycookiecontainer just created
Stream myrequeststream = myhttpwebrequest. getrequeststream ();
Streamwriter mystreamwriter = new streamwriter (myrequeststream, encoding. getencoding ("gb2312 "));
Mystreamwriter. Write (indata );
// Write data to the request stream of httpwebrequest
Mystreamwriter. Close ();
Myrequeststream. Close ();
// Close the open object
Httpwebresponse myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
// Create an httpwebresponse
Myhttpwebresponse. Cookies = mycookiecontainer. getcookies (myhttpwebrequest. requesturi );
// Obtain the cookiecollection of a cookie set containing a URL
Stream myresponsestream = myhttpwebresponse. getresponsestream ();
Streamreader mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
Outdata = mystreamreader. readtoend ();
// Read the data from the response stream of httpwebresponse
Mystreamreader. Close ();
Myresponsestream. Close ();
Console. writeline ("Get verification code" + outdata );
// Step 2: log on to the system to obtain the cookie
String verifycode = outdata. substring (outdata. lastindexof ("ID =/" verifycode/"") + 63,4 );
Url = "http: // localhost: 8001/checklogin. asp ";
Indata = "username = testuser & Password = testpassword & verifycode =" + verifycode;
Outdata = "";
// Create a cookiecontainer to store the cookie set
Myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
// Create an httpwebrequest
Myhttpwebrequest. Referer = "http: // localhost: 8001/login. asp ";
Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";
Myhttpwebrequest. contentlength = indata. length;
Myhttpwebrequest. method = "Post ";
Myhttpwebrequest. cookiecontainer = mycookiecontainer;
// Set cookiecontainer of httpwebrequest to the mycookiecontainer just created
Myrequeststream = myhttpwebrequest. getrequeststream ();
Mystreamwriter = new streamwriter (myrequeststream, encoding. getencoding ("gb2312 "));
Mystreamwriter. Write (indata );
// Write data to the request stream of httpwebrequest
Mystreamwriter. Close ();
Myrequeststream. Close ();
// Close the open object
Myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
// Create an httpwebresponse
Myhttpwebresponse. Cookies = mycookiecontainer. getcookies (myhttpwebrequest. requesturi );
// Obtain the cookiecollection of a cookie set containing a URL
Myresponsestream = myhttpwebresponse. getresponsestream ();
Mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
Outdata = mystreamreader. readtoend ();
// Read the data from the response stream of httpwebresponse
Mystreamreader. Close ();
Myresponsestream. Close ();
Console. writeline ("Login System" + outdata );
// Step 2: Try to view the page that requires login
// Get the cookie after login, and then send the page request that requires login to directly read the Post-login content.
Url = "http: // localhost: 8001/wowprice. asp ";
Myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
Myhttpwebrequest. cookiecontainer = mycookiecontainer;
// The cookiecontainer that just now has a cookie, which can be directly verified by attaching it to httpwebrequest.
Myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
Myhttpwebresponse. Cookies = mycookiecontainer. getcookies (myhttpwebrequest. requesturi );
Myresponsestream = myhttpwebresponse. getresponsestream ();
Mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
Outdata = mystreamreader. readtoend ();
Mystreamreader. Close ();
Myresponsestream. Close ();
Console. writeline ("Get Price page" + outdata );
This. button4.enabled = true;
}

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.