zoukankan      html  css  js  c++  java
  • HttpWebRequest提交(Post)数据

    protected void Page_Load(object sender, EventArgs e)
            {
                string sql = "select top 1 * from [user] order by userid asc";
                DataTable dt = DBUtility.SQLHelperJJ3.Query(sql).Tables[0];
                string type = "0";
                string username = dt.Rows[0]["username"].ToString();
                string password = dt.Rows[0]["password"].ToString();
                string email = dt.Rows[0]["email"].ToString();
                this.HttpPost(type, username, password, email);
            }
    
            private void HttpPost(string type, string username, string password, string email)
            {
                string content = "type=" + type + "&username=" + username + "&password=" + password + "&email=" + email;
                string url = "http://localhost/php/index.php?Action=BatchRegist";
                try
                {
                    //获取提交的字节
                    byte[] bs = Encoding.UTF8.GetBytes(content);
                    //设置提交的相关参数
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded"; 
                    req.ContentLength = bs.Length;
                    //提交请求数据
                    Stream reqStream = req.GetRequestStream();
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Close();
                    //接收返回的页面,必须的,不能省略
                    WebResponse wr = req.GetResponse();
                    System.IO.Stream respStream = wr.GetResponseStream();
                    System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
                    string t = reader.ReadToEnd();
                    System.Web.HttpContext.Current.Response.Write(t);
                    wr.Close();
                }
                catch (Exception ex)
                {
                    System.Web.HttpContext.Current.Response.Write("异常在getPostRespone:" + ex.Source + ":" + ex.Message);
                }
            }
  • 相关阅读:
    [bzoj3224] 普通平衡树
    [总结] 三种常见的区间贪心问题
    [NOIP2014] 飞扬的小鸟
    POJ 1185 炮兵阵地
    WOJ 1538 Stones II 转化背包问题
    WOJ 1542 Countries 并查集转化新点+最短路
    UVA 11375 高精度Bign类
    2014_csu选拔1_B
    Codeforces 405D 数学问题
    Codeforces 400C 矩阵乘法 数学规律
  • 原文地址:https://www.cnblogs.com/shiningrise/p/10912362.html
Copyright © 2011-2022 走看看