zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    DOS系统功能调用与BIOS中断调用 [转自KingofCoders]
    纯手工保护光盘数据(转)
    程序员不错的建议【转】
    初识逆向技术(转)
    Notepad++插件推荐JSMin
    jQuery Ready 与 Window onload 的区别
    Javascript typeof和instanceof判断数据类型
    浅谈Javascript 中几种克隆(clone)方式
    Javascript Array sort排序问题
    不同浏览器对display为none元素的图片处理不一样
  • 原文地址:https://www.cnblogs.com/kevinhome/p/3850398.html
Copyright © 2011-2022 走看看