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;
}