[Original] C #. NET network programming post data to the website
Today, a voting software was created, mainly to post the specified data to the specified page to automatically increase the number of votes in the background.
Next I will post my code. For security purposes, I have hidden all relevant real URLs.
The following is a reference to the namespace: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. IO; // access the data stream.
Using system. text;
Using system. Text. regularexpressions; // regular expression used to verify whether the returned webpage contains the target data
Using system. Windows. forms;
Using system. Net; // required. This namespace is used for webpage access.
A function is implemented to hide sensitive data (using asterisks) Public Boolean voteonce () for security purposes ()
{
// Create an httpwebrequest for sending a request
Httpwebrequest hwrq = (httpwebrequest) webrequest. Create ("http: // www. ***. com/***. asp? * ** = *** & *** = ***");
// The following describes the data headers and data sending methods.
Hwrq. accept = "application/X-Shockwave-flash, image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord ,*/*";
Hwrq. Referer = "http: // www. ***. com/***. asp? * ** = *** & *** = ***";
Hwrq. contenttype = "application/X-WWW-form-urlencoded ";
Hwrq. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1;. Net CLR 2.0.50727; Maxthon 2.0 )";
Hwrq. keepalive = true;
Hwrq. method = "Post ";
// Send the following data and use minisnifer to capture packets to analyze what data should be sent
String poststr = "* = ***** % ** & ***** = *";
Asciiencoding asc2e = new asciiencoding ();
Byte [] bytepost = asc2e. getbytes (poststr); // convert the data to be sent into bytes.
Hwrq. contentlength = bytepost. length;
// Below is the byte stream for sending data
Stream mystream = hwrq. getrequeststream ();
Mystream. Write (bytepost, 0, bytepost. Length );
Mystream. Close (); // remember to end the byte stream.
// Create an httpwebresponse instance
Httpwebresponse HWRP = (httpwebresponse) hwrq. getresponse ();
Streamreader mystreamr = new streamreader (HWRP. getresponsestream (), encoding. Default );
String result = mystreamr. readtoend ();
Boolean r = false;
If (RegEx. ismatch (result, "successful") = true)
{
R = true;
}
Mystreamr. Close ();
Return R;
}