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();

  • 相关阅读:
    关于商业智能(Business Intelligence,简称BI)的认识
    Python连接mysql数据库和关闭数据库的方法
    Python 列表list方法clear( )和直接list [ ]的区别
    截止今天学习大数据技术的笔记
    【已解决】hive导出mysql报错:Container [pid=3962,containerID=container_1632883011739_0002_01_000002] is running 270113280B beyond the 'VIRTUAL' memory limit.
    sqoop安装配置以及简单使用
    大数据相关常用命令行或操作
    阿里巴巴数据库设计规范
    【已解决】linux环境jps命令不显示进程
    【已解决】初始化 Hive 元数据库报错slf4j-log4j12-1.7.25.jar包冲突
  • 原文地址:https://www.cnblogs.com/kevinhome/p/3850398.html
Copyright © 2011-2022 走看看