C # Simulate submission form in post

Source: Internet
Author: User

This is what I wrote a year ago. A code that simulates a post-submission form in C # is now recorded below to avoid forgetting. At that time just learned c#~ flowing away quickly. Very unfamiliar. The code looks very childish and bloated.

#region content Add function (Contentinsert)
public string Contentinsert (string BookID, String booktitle, String bookcontent,string taskurl,string ztagend)
{
String uristring = "Here is the page that takes viewstate equivalent";

If it's an ASP, it's best to take the ViewState and eventvalidation values and submit them together.
///////////////////////////////////////
Opens the specified page
///////////////////////////////////////
XMLRW Updatebook = new XMLRW ();
WebClient WebClient = new WebClient ();
byte[] ResponseData = Webclient.downloaddata (uristring);
String srcstring = Encoding.UTF8.GetString (responsedata);
///////////////////////////////////////
Fill out the page and submit
///////////////////////////////////////
WebClient = new WebClient ();
WEBCLIENT.HEADERS.ADD ("Content-type", "application/x-www-form-urlencoded"); Defines the associated header

               //Get the veiwstate of the page    this value and the value of eventvalidation are used here to receive data from the data receiving page               
                 String viewstateflag = "id=\" __viewstate\ "value=\" "; Dynamically fetching the value of the __viewstate  
                 int i = Srcstring.indexof (viewstateflag) + viewstateflag.length; 
                 Int J = srcstring.indexof ("\" ", i);  
                string viewState = Srcstring.substring (i, j-i);

Gets the page eventvalidation this value and the value of eventvalidation here for the data receive page to receive the data
String eventvalidationflag = "id=\" __eventvalidation\ "value=\" "; Dynamic fetching of __eventvalidation\ values
i = Srcstring.indexof (eventvalidationflag) + eventvalidationflag.length;
j = srcstring.indexof ("\" ", I);
String eventvalidation = Srcstring.substring (i, j-i);
Get the number of chapters in a page
String viewmenuid = "<input name=\" txt_menuid\ "type=\" text\ "value=\" "; Dynamic fetching of __viewstate values
i = Srcstring.indexof (viewmenuid) + Viewmenuid. Length;
j = srcstring.indexof ("\" ", I);
String txt_menuid = Srcstring.substring (i, j-i);
/* Text of the Submit button
String Submitbutton = "Commit";
* This code is mainly used when the data receiving page is a button
* */
BookTitle = System.Web.HttpUtility.UrlEncode (booktitle);
Bookcontent = System.Web.HttpUtility.UrlEncode (bookcontent);
ViewState = System.Web.HttpUtility.UrlEncode (viewState);
Eventvalidation = System.Web.HttpUtility.UrlEncode (eventvalidation);
Txt_menuid = System.Web.HttpUtility.UrlEncode (Txt_menuid);
The string data to commit. Format: user=uesr1&password=123
The following begins the execution of the data submission when the commit no error will return the submitted page code back
String poststring = "txt_title=" + booktitle + "&txt_content=" + bookcontent + "&txt_menuid=" + Txt_menuid + "&amp ; cmdsaveconn.x=0&cmdsaveconn.y=0 "+" &__viewstate= "+ VIEWSTATE +" &__eventvalidation= "+ EVENTVALIDATION;
/*txt_title is the title content, Bookcontent is the content Txt_menuid is the current number of chapters cmdsaveconn.x=0&cmdsaveconn.y=0 is used to pass ImageButton control pass parameters Submission for data receive page for different buttons with parameters to achieve the touch to the general see: Http://hi.baidu.com/zeratul_bb/blog/item/34d9f7fda770c241d6887deb.html __viewstate and _eventvalidation is an ASP. NET-specific hidden value pass, which is now thought to pass data ~ Unknown origin */
Convert a string into a byte array
byte[] PostData = Encoding.ASCII.GetBytes (poststring);
Upload data, return the byte array of the page
ResponseData = Webclient.uploaddata (uristring, "POST", postdata); At the core of this function, the main solution here is to use the Post method to pass the data to simulate the form submission ~ There is also to avoid the submission after the return to the data received the specified page ~ is a good way!! See: Http://www.cnblogs.com/anjou/archive/2006/12/25/602943.html (WebClient filling and submitting forms in ASP. )
Converts the returned byte array into a string (HTML);
The page returned by ASP. NET is generally Unicode, and if it is simplified Chinese should use
Encoding.GetEncoding ("GB2312"). GetString (ResponseData)
srcstring = Encoding.UTF8.GetString (responsedata);
Updatebook.updatexml (Taskurl, BookID, ztagend);
Return "successfully added data, the return data byte is:" +srcstring.length.tostring (); Returns the page code length of the data submission to the keynote function!

///////////////////////////////////////
Parse the returned page
///////////////////////////////////////
// ...... ......
}
#endregion

-----------------------------

According to the different buttons summarized as follows:

There are two scenarios in which the server-side events are triggered on the client in asp:

A. button in WebControls and type in HtmlControls is the HtmlInputButton of the Submit

The two buttons end up in the form of a client: <input name= "Submit1" id= "Submit1" type= "Submit" value= "Submit", which is the Submit button for form form, The click is then sent as a parameter to the server, the parameter is this: the control's Name property = The value of the control, corresponding to the above example is: submit1= Submit. The server will be informed by this key of the Name property of the received control that the button was clicked, thus triggering the button's click event on the server.

Two. ImageButton or httpinputimage:

These controls behave like this to the client: <input type=image id=img1>, and clicking on such a control will submit the form directly, acting with the submit button. Click on such a control to upload to the server parameter is this: ID.X=NN&ID.Y=NN, corresponding to the above example is: Img1.x=nn&img1.y=nn

Three. The type in HtmlControls is the HtmlInputButton of the button and all other control events, such as LinkButton Click, TextBox's Change event, and so on:

These events are sent to the server by a unified mechanism after the client has been generated.

1. First the ASP. NET page framework uses two hidden fields to hold the event that represents which control was fired, as well as the parameters of the event:

<!-represents the control that triggered the event, which is typically the name--

<input type= "hidden" name= "__eventtarget" value= "/>

<!-represents a parameter that triggers an event, typically when a control has more than two events, to distinguish which event--

<input type= "hidden" name= "__eventargument" value= "/>

2. The server generates a JScript method to handle the sending of all these events, the code is:

<script language= "JavaScript" >

<!--

function __doPostBack (Eventtarget, eventargument) {

var theform = document. WebForm2;

Theform.__eventtarget.value = Eventtarget;

Theform.__eventargument.value = eventargument;

Theform.submit ();

}

-

</script>

3. Each control that raises a server event invokes the above code in the client-side event of the response:

For example, the type in HtmlControls is the Click event of the button's HtmlInputButton

The <!-client's Click event Call __dopostback,eventtarget parameter is ' Button2 ', which indicates that the event is triggered by name ' Button2 ' control, eventargument is empty, Indicates that this type is a button HtmlInputButton only one client-triggered server event---

<input language= "javascript" onclick= "__doPostBack (' Button2 ', ')" Name= "Button2" id= "Button2" type= "button" value = "button"/>

Also, for example, the Change event of a TextBox control

The onchange event call __dopostback,eventtarget parameter of the <!-client is ' TextBox1 ', which indicates that the event is triggered by name ' TextBox1 ' control. The TextBox control has only one client-triggered service-side event TextChanged, so the server will trigger this textbox's TextChanged event

<input name= "TextBox1" type= "text" id= "TextBox1" onchange= "__dopostback (' TextBox1 ', ')" language= "JavaScript"/ >

4. After the client triggers the event, the __doPostBack method is called, and the Eventtarget and event arguments that represent the triggered control source eventargument are paid to two hidden fields __eventtarget and __eventargument, respectively. The form is then submitted to determine which control's event was triggered by the server based on __eventtarget and __eventargument.

C # Simulate submission form in post

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.