使用 Post 方法传递数据

来源:互联网 发布:ipadpro必备软件推荐 编辑:程序博客网 时间:2024/06/09 16:09

使用 Post 方法传递数据 --ASP.net

示例在paike.3g.cn/zdtu/TestRecPostData.aspx
以及CommonBg/Test/TestPostData.aspx
------------------------------------------------
---1,请求页面,即POST页面----

    private string sendUrl = "http://paike.3g.cn/zdtu/TestRecPostData.aspx";
    private string postdata = @"name=tuzuodong&data=英国暴风雪导致孕妇数量增加

 

 

PostData(postdata, sendUrl);

    public void PostData(string postData,string recUrl)
    {
        UTF8Encoding code = new UTF8Encoding();  //这里采用UTF8编码方式
        //string postData = "aa=iceapple.net&bb=yibin.net"; //这是要post的数据
        byte[] data = code.GetBytes(postData);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(recUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded"; //这里的ContentType很重要!
        request.ContentLength = data.Length;
        using (Stream stream = request.GetRequestStream()) //获取数据流,该流是可写入的
        {
            stream.Write(data, 0, data.Length); //发送数据流
            stream.Close();
        }
    }


-----2, 接收POST数据页面-----------------

 string postData="";
 string name="";
 //name = Request.QueryString["name"] as string;  //post方法不支持GET的接收方式,必须用Request.Form的方式
 //postData = Request.QueryString["data"] as string; //post方法不支持GET的接收方式,必须用Request.Form的方式

 if(Request.Form["name"]!=null)
 {
  name = Request.Form["name"].ToString();
 }
 if(Request.Form["data"]!=null)
 {
  postData = Request.Form["data"].ToString();
 }

参考:http://www.cftea.com/c/2008/07/B0AUNSSUA1NQCVIF.asp
 

原创粉丝点击