First read this section Code :
/**/ /// <Summary>
/// Submit data to remote server
/// </Summary>
/// <Param name = "strurl"> URL to be submitted </Param>
/// <Param name = "postdata"> Submitted data </Param>
Public Void Postdata ( String Strurl, String Postdata)
{
Byte [] Data = System. Text. encoding. getencoding ( " Gb2312 " ). Getbytes (postdata );
Httpwebrequest myrequest =
(Httpwebrequest) webrequest. Create (strurl );
Myrequest. useragent = " Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 5.0) " ;
Myrequest. cookiecontainer = New Cookiecontainer ();
Myrequest. Method = " Post " ;
Myrequest. contenttype = " Application/X-WWW-form-urlencoded " ;
Myrequest. contentlength = Data. length;
System. Io. Stream newstream = Myrequest. getrequeststream ();
// Submit data
Newstream. Write (data, 0 , Data. Length );
Newstream. Close ();
/**/ /******* After the data is submitted, the following information is returned from the submitted web page for analysis and submission (for example, whether the identity authentication is valid or not )*****/
Httpwebresponse response = (Httpwebresponse) myrequest. getresponse ();
System. Io. streamreader SR = New System. Io. streamreader (response. getresponsestream (), system. Text. encoding. getencoding ( " Gb2312 " ));
String Readtext = Sr. readtoend ();
Response. Close ();
}
Call this method where you need to submit data: String Posturl = " Http: // localhost/webform1.aspx " ;
String Postdata = " _ Viewstate = ddwtmjyxnjy2nze5oztspenozwnrqm94mts % 2bpusivwzz1bd40pjkomf6p0ajgunc & textbox1 = 1234 & textbox2 = fig = button " ;
Postdata (posturl, postdata );
Note that postdata contains a _ viewstate, which must be filled in correctly. Otherwise, the server does not recognize it... (Of course, this is for Asp.net, but no matter what kind of site, it is necessary to cut data packets, and then analyze the replacement content, in order to correctly post to the other site.
How to intercept, can refer to: http://hqt.cnblogs.com/archive/2005/10/08/250277.html
The webform1 page is just a simple test with one textbox and one button
Button#click in the background file:
Private Void Button#click ( Object Sender, system. eventargs E)
{
If (Textbox1.text = " 123 " )
{
Response. Write ("<SCRIPT> alert ('good'); </SCRIPT>");
}
}
The result returned by the server is as follows: < Script > Alert ('good '); </ Script >
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en" >
< Html >
< Head >
< Title > Webform2 </ Title >
< Meta Name = "Generator" Content = "Microsoft Visual Studio. net7.1" >
< Meta Name = "Code_language" Content = "C #" >
< Meta Name = "Vs_defaultclientscript" Content = "JavaScript" >
< Meta Name = "Vs_targetschema" Content = "Http://schemas.microsoft.com/intellisense/ie5" >
</ Head >
< Body Ms_positioning = "Gridlayout" >
< Form Name = "Form1" Method = "Post" Action = "Webform2.aspx" ID = "Form1" >
< Input Type = "Hidden" Name = "_ Viewstate" Value = "Ddwymde4oti5mdg4ozs + vx7wtwfh0li152lrz3jpbmxtinq =" />
< Input Name = "Textbox1" Type = "Text" Value = "123" ID = "Textbox1" Style = "Z-INDEX: 102; left: 328px; position: absolute; top: 176px" />
< Input Type = "Submit" Name = "Button1" Value = "Button" ID = "Button1" Style = "Z-INDEX: 103; left: 328px; position: absolute; top: 240px" />
</ Form >
</ Body >
</ Html >
If the session judgment is added to the button#click and the session value is obtained from another page, how can this problem be solved?
1. get access to the page for obtaining the session
2. Submit the page for session verification.