The following code is the code below my post button, which has some parameters and return data for my actual project, for reference only.
if (request.querystring["po"]! = "")
{
Strpo = request.querystring["Po"]. ToString ();
}
String url = HttpContext.Current.Request.Url.AbsoluteUri.ToString (). Replace ("popage.aspx?po=" + strpo.tostring () + "", "popage.aspx?po=" + strpo.tostring () + "");
Server.Transfer (URL);
Cookiecontainer Mycookiecontainer = new Cookiecontainer ();
HttpWebRequest request = webrequest.create (URL) as HttpWebRequest;
Request. Cookiecontainer = Mycookiecontainer;
Request. Method = "GET";
Request. KeepAlive = false;
HttpWebResponse response = Request. GetResponse () as HttpWebResponse;
System.IO.Stream Responsestream = Response. GetResponseStream ();
System.IO.StreamReader reader = new System.IO.StreamReader (Responsestream, Encoding.UTF8);
String srcstring = reader. ReadToEnd ();
Get the page ViewState
String viewstateflag = "id=\" __viewstate\ "value=\" ";
int i = Srcstring.indexof (viewstateflag) + viewstateflag.length;
Int J = srcstring.indexof ("\" ", I);
String viewState = Srcstring.substring (i, j-i);
Get page Eventvalidation
String eventvalidationflag = "id=\" __eventvalidation\ "value=\" ";
i = Srcstring.indexof (eventvalidationflag) + eventvalidationflag.length;
j = srcstring.indexof ("\" ", I);
String eventvalidation = Srcstring.substring (i, j-i);
String Submitbutton = "Btnsubmit";
//Form infromation
String strversion = MaxVersion (Strpo);
String strponum = Strpo;
String strpotype = "ZN B ";
String strchangdate = DateTime.Now.ToString ("Yyyy-mm-dd");
String strsupplier = DpList1.SelectedValue.ToString ();
String strhopedate = TbjhDate.Text.ToString ();
String strqty = (this.) Page.findcontrol ("TB" + "1" + "2") as TextBox). Text.tostring ();
//Convert the text into the URL encoding string
ViewState = System.Web.HttpUtility.UrlEncode (viewState);
Eve Ntvalidation = System.Web.HttpUtility.UrlEncode (eventvalidation);
Submitbutton = System.Web.HttpUtility.UrlEncode (Submitbutton);
Concat the string data which would be submit
String formatString =
"Po_version={0}&po_num={1}&loginbutton={2}&po_type={3}&change_date={4}&supplier_name={5} &delivery_date={6}&buttons_lable={7}&__viewstate={8}&__eventvalidation={9} ";
String poststring =
String. Format (formatString, Strversion, Strpo, Submitbutton, Strpotype, Strchangdate, Strsupplier, Strhopedate, Strqty, ViewState, eventvalidation);
Convert the submit string data into the byte array
byte[] PostData = Encoding.ASCII.GetBytes (poststring);
Set the request parameters
Request = WebRequest.Create (URL) as HttpWebRequest;
Request. Method = "POST";
Request. Referer = URL;
Request. KeepAlive = false;
Request. useragent = "mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0; infopath.2; CIBA) ";
Request. ContentType = "application/x-www-form-urlencoded";
Request. Cookiecontainer = Mycookiecontainer;
System.Net.Cookie ck = new System.Net.Cookie ("TestCookie1", "Value of Test Cookie");
Ck. Domain = Request. Requesturi.host;
Request. Cookiecontainer.add (CK);
Request. Cookiecontainer.add (response. Cookies);
Request. ContentLength = Postdata.length;
String strSQL = String. Format ("INSERT INTO Dbo.po_podetail (po_version,po_num,po_type,change_date,supplier_name,delivery_date,buttons_ lable) VALUES (' {0} ', ' {1} ', ' {2} ', ' {3} ', ' {4} ', ' {5} ', ' {6} ') ", Strversion, Strpo, Strpotype, Strchangdate, Strsupplier, Strhopedate, Strqty);
Strcon. ExecuteSQL (strSQL);
Submit the request data
System.IO.Stream outputstream = Request. GetRequestStream ();
Request. AllowAutoRedirect = true;
Outputstream.write (postdata, 0, postdata.length);
Outputstream.close ();
Get The return Data
Response = Request. GetResponse () as HttpWebResponse;
Responsestream = Response. GetResponseStream ();
reader = new System.IO.StreamReader (Responsestream, Encoding.UTF8);
srcstring = reader. ReadToEnd ();
Response.Write (srcstring);
Response.End ();