[Original] C #. NET network programming post data to the website

Source: Internet
Author: User
[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;
}

Related Article

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.