Httpwebrequest simulates a browser to send a request

Source: Internet
Author: User

The request data captured by the tool is to send post data to the target servlet:

// Post/AAA/bbbform HTTP/1.1
// Host: www.testtest.cn
// Connection: keep-alive
// Content-Length: 265
// Origin: http://www.testtest.cn
// X-requested-with: XMLHttpRequest
// User-Agent: Mozilla/5.0 (Windows NT 6.1) applewebkit/535.19 (khtml, like gecko) Chrome/18.0.1025.162 Safari/535.19
// Content-Type: Application/X-WWW-form-urlencoded; charset = UTF-8
// Accept: Application/JSON, text/JavaScript, */*; q = 0.01
/Referer: http://www.testtest.cn
// Accept-encoding: gzip, deflate, SDCh
// Accept-language: ZH-CN, ZH; q = 0.8
// Accept-charset: GBK, UTF-8; q = 0.7, *; q = 0.3
// COOKIE: JSESSIONID = 684dbac1bd970d4c614e3d5410a5086d; bt2pxxhzg9 = mdawm2iyngm1zwmwmdawmdawmdywsg4xnesxmzm1mjy4ode1

// Results = 1223088 & Results = 1223107 & Results = 1223065 & Results = 1223123
// & Username = % E9 % 83% 91% E7 % be % A4 & idcard = 11111117 & Email = & imagevalue = 1

The following code assembles and sends a request:

System. net. servicepointmanager. expect100continue = false;
String url = "HTTP: // Www.testtest.cn// AAA/bbbform ";
Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
// Fill in the basic information of httpwebrequest
Request. method = "Post ";
String username = "Zhang San"; // encoding. getencoding ("UTF-8"). String. empty;
String idcard = "12222 ";

String postdata = string. format (@ "Results = 1223050 & Results = 1223088 & Results = 1223107 & Results = 1223065 & Results = 1223123 & username = {0} & idcard = {1} & Email = & imagevalue = 4 ", username, idcard );
Byte [] bytestopost = encoding. getencoding ("UTF-8"). getbytes (postdata );
Request. contentlength = bytestopost. length;

Request. headers. Set ("cookie", "JSESSIONID = 684dbac1bd970d4c614e3d5410a5086d; bt2pxxhzg9 = Success ");
// Cookiecontainer = new cookiecontainer ();
// Request. cookiecontainer = cookiecontainer;
// Cookiecontainer. setcookies (New uri (URL, urikind. Absolute), "JSESSIONID = 20dffca8d3b00846e1104700e1f4d894 ");
// Cookiecontainer. Add (New uri (URL, urikind. Absolute), new cookie ("bt2pxxhzg9", "success "));

Request. useragent = "Mozilla/5.0 (Windows NT 6.1) applewebkit/535.19 (khtml, like gecko) Chrome/18.0.1025.162 Safari/535.19 ";
Request. contenttype = "application/X-WWW-form-urlencoded; charset = UTF-8 ";
Request. Accept = "application/JSON, text/JavaScript, */*; q = 0.01 ";
Request. Host = "www.testtest.cn ";
Request. Referer = "http://www.testtest.cn ";
Request. headers. Set ("X-requested-with", "http://www.testtest.cn ");
Request. headers. Set ("Origin", "XMLHttpRequest ");
Request. headers. Set ("Accept-encoding", "gzip, deflate, SDCh ");
Request. headers. Set ("Accept-language", "ZH-CN, ZH; q = 0.8 ");
Request. headers. Set ("Accept-charset", "GBK, UTF-8; q = 0.7, *; q = 0.3 ");

Stream requeststream = request. getrequeststream ();
Requeststream. Write (bytestopost, 0, bytestopost. Length );
Requeststream. Close ();
Stream responsestream;

Try
{
Responsestream = request. getresponse (). getresponsestream (); // receives the request result
Streamreader reader = new streamreader (responsestream );
String result = reader. readtoend ();
Txtresult. Text = result;
}
Catch (exception ex)
{
System. Diagnostics. Debug. Write (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS fff") + ":" + ex. Message );
}

This method successfully simulates the request and receives the request result.

However, in the end, this method is not used to implement the requirement, because the verification code of the other party is

(1) Each time a request is sent, the verification code is re-obtained and there is a line of interference, which cannot be identified by a simple program.

(2) After the request is submitted, the verification code will be cleared (to prevent future exploitation)

This is also a way to prevent violent submissions.

 

The subsequent implementation method was to manually submit in webbrowser, but the control values in the page were automatically filled, thus improving the submission efficiency.

The method is as follows:

List <string> liststr = new list <string> ();
Private void btnrefrechpage_click (Object sender, eventargs E)
{
Try
{
If (liststr. Count> 0)
{
Txtresult. Text = (100-liststr. Count). tostring ();
String [] value = system. Text. regularexpressions. RegEx. Split (liststr [0], @ "\ t ");
// Search for HTML elements and assign values
Webbrowser. Document. getelementbyid ("username"). innertext = value [0];
Webbrowser. Document. getelementbyid ("idcard"). innertext = value [1];
Htmlelementcollection checkbox = webbrowser. Document. getelementsbytagname ("input ");
Foreach (htmlelement element in checkbox)
{
If (", hour, hour, 1223210,1223110, 1223098,1223050, 1223088,1223107, 1223065,1223123"). indexof (element. getattribute ("value"). tostring ()>-1)
{
Element. setattribute ("checked", "true ");
}
}
Webbrowser. Document. getelementbyid ("username"). Focus ();
Liststr. removeat (0 );
}
}
Catch
{}
}

Read filling information:

Private void btnreaduser_click (Object sender, eventargs E)
{
Liststr = new list <string> ();
Filestream file = new filestream ("C:/id.txt", filemode. Open );
Streamreader reader = new streamreader (file, encoding. getencoding ("gb2312 "));
String STR = string. empty;
While (STR = reader. Readline ())! = NULL)
{
Liststr. Add (STR );
}
Reader. Close ();
Reader. Dispose ();
File. Close ();
File. Dispose ();
}

 

Private void btnlink_click (Object sender, eventargs E)
{
Webbrowser. url = new uri (txturl. Text. Trim (), urikind. Absolute );
Webbrowser. Refresh ();
Webbrowser. documentcompleted + = new webbrowserdocumentcompletedeventhandler (webbrowser_documentcompleted );
}

Void webbrowser_documentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)
{
Try
{
Currenturl. Text = webbrowser. url. absoluteuri. tostring ();
}
Catch {}
}

 

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.