C#中Get 与 Post提交数据的总结示例(源代码)

用Get 与 Post提交数据是我们经常做的事情,在.net里面是如何实现的呢?

我们知道在Form里面,可以使用post也可以使用get。它们都是method的合法取值。但是,post和get方法在使用上下面几点不同: 1、Get方法通过URL请求来传递用户的输入。Post方法通过另外的形式。 2、Get方式的提交你需要用Request.QueryString来取得变量的值,而Post方式提交时,你必须通过Request.Form来访问提交的内容。

1. get是从服务器上获取数据,post是向服务器传送数据。 2.  get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。

     post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。 3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form(可以缩写为Request)获取提交的数据。 4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。 5. get安全性较低,post安全性较高。 

下面用Get和Post2种方式演示如何提交数据

GetDataSend   Get 提交数据

GetDataReceive  接收get的请求数据 

PostDataSend   Post 提交数据

PostDataReceive  接收Post的请求数据 

GetDataSend.aspx
复制代码
< form id = " form1 " action = " GetDataReceive.aspx " > < div > < input type = " text " name = " title " value = " tt " /> < input type = " text " name = " desc " value = " content " /> < input type = submit value = " GET方式 " > </ div > </ form >
复制代码

 

GetDataReceive.aspx
复制代码
  protected void Page_Load( object sender, EventArgs e) {   string strTitle = HttpContext.Current.Request.QueryString[ " Title " ];
string strContent = Request.QueryString[ " Desc " ];
// QueryString可获得url中的参数 string strRes = " This is the response from the server:\r\n "
+ " 标题为:, " + strTitle + " \r\nDesc为 " + strContent + " ! " ;
HttpContext.Current.Response.Clear();
// 清除缓冲区流中的所有内容输出。 HttpContext.Current.Response.Write(strRes); // 将信息写入 HTTP 响应输出流。 HttpContext.Current.Response.Flush(); // 向客户端发送当前所有缓冲的输出。
//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 EndRequest 事件(停止请求)。 HttpContext.Current.Response.End();
  }
复制代码

 

PostDataSend.aspx
复制代码
< form id = " form1 " runat = " server " method = " post " > < div > 标题: < asp:TextBox ID = " TextBox1 " runat = " server " ></ asp:TextBox >< br /> 内容: < asp:TextBox ID = " TextBox2 " runat = " server " ></ asp:TextBox >< br />
< asp:Button ID = " Button1 " runat = " server " Text = " Button " onclick = " Button1_Click " />
</ div > </ form >

protected void Button1_Click( object sender, EventArgs e) { string strTitle = TextBox1.Text; string strContent = TextBox2.Text;
try { Encoding encode = Encoding.GetEncoding( " utf-8 " );
string postData = " Title= " + strTitle; string strURL = " http://localhost/Test/PostData/PostDataReceive.aspx " ; postData += ( " &Desc= " + strContent);
byte [] data = encode.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL); myRequest.Method = " POST " ; // myRequest.ContentType = "application/x-www-form-unlencoded"; // request.ContentType = "text/xml; charset=utf-8 "; myRequest.ContentType = " application/x-www-form-urlencoded " ; // ContentType很重要! myRequest.ContentLength = data.Length;
Stream newStream
= myRequest.GetRequestStream(); newStream.Write(data, 0 , data.Length); newStream.Close(); Response.Redirect( " PostDataReceive.aspx " );
}
catch (Exception ex) { Response.Write( " <script>alert(\ "" + ex.Message + " \ " )</script> " ); } }
复制代码

 

PostDataReceive.aspx
复制代码
protected void Page_Load( object sender, EventArgs e) { if (Request.ServerVariables[ " Request_Method " ].ToString() == " POST " ) { string strTitle = Request.Form[ " Title " ]; string strContent = Request.Form[ " Desc " ]; if (File.Exists(Server.MapPath( " . " ) + " \\MyPostFile.txt " ) == false ) { File.CreateText(Server.MapPath( " . " ) + " \\MyPostFile.txt " ); } StreamWriter SW; SW = File.AppendText(Server.MapPath( " . " ) + " \\MyPostFile.txt " ); SW.WriteLine(strTitle); SW.WriteLine(strContent); SW.Close(); SW = null ; } else { StreamReader SR; string S; SR = File.OpenText(Server.MapPath( " . " ) + " \\MyPostFile.txt " ); S = SR.ReadLine(); while (S != null ) { Response.Write(S + " \r\n " ); S = SR.ReadLine(); } SR.Close(); SR = null ; } }
复制代码

转载于:https://www.cnblogs.com/tenghaibo/p/3558293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值