ASP.Net 提交表单 post 方式代码

 

下面代码是我post按钮下面的代码,里面有我实际项目的一些参数和返回数据,仅供参考。

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 = "ZNB";
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);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
submitButton = System.Web.HttpUtility.UrlEncode(submitButton);

// Concat the string data which will 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();

ASP.Net 提交表单 post 方式代码,布布扣,bubuko.com

ASP.Net 提交表单 post 方式代码

上一篇:Ubuntu安装使用MySQL


下一篇:[C#.NET] C#中使用反射的性能分析