.net 后台提交表单,获取返回结果

a.aspx后台提交表单,b.aspx接收表单(根据input的name获得值)

1、a.aspx

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  8.     <title></title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.     <div>  
  13.         <asp:TextBox ID="name" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  14.         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html>  

后台代码

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. public partial class a : System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.   
  16.     }  
  17.     protected void Button1_Click(object sender, EventArgs e)  
  18.     {  
  19.         //如果表单中要发送中文,可以对数据进行编码gb2312/gbk。  
  20.   
  21.         Encoding myencode = Encoding.GetEncoding("gb2312");  
  22.   
  23.         //然后处理要传的表单数据。  
  24.         string strpost = HttpUtility.UrlEncode("name_c", myencode) + "=" + HttpUtility.UrlEncode(name.Text, myencode)+"&"  
  25.            + HttpUtility.UrlEncode("name_e", myencode) + "=" + HttpUtility.UrlEncode(name.Text+"测试", myencode);  
  26.         //string strpost = "name_c=" + name.Text + "&" + "name_e=" + name.Text;  
  27.         //有多个参数可以用"&"拼接。  
  28.   
  29.         //接着序列化参数。  
  30.   
  31.         byte[] postBytes = Encoding.ASCII.GetBytes(strpost);  
  32.   
  33.         //创建请求示例。  
  34.   
  35.         HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:61444/b.aspx");  
  36.   
  37.         //下面可以选择请求的方式,标头。  
  38.   
  39.         req.Method = "POST";  
  40.   
  41.         req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";  
  42.   
  43.         req.ContentLength = postBytes.Length;  
  44.   
  45.         using (Stream sendStream = req.GetRequestStream())  
  46.         {  
  47.   
  48.             sendStream.Write(postBytes, 0, Convert.ToInt32(req.ContentLength));  
  49.   
  50.         }  
  51.         using(WebResponse wr=req.GetResponse())  
  52.         {  
  53.             Stream respStream = wr.GetResponseStream();  
  54.             using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding("utf-8")))  
  55.             {  
  56.                 Label1.Text = reader.ReadToEnd();  
  57.             }  
  58.         }  
  59.     }  
  60. }  

2、b.aspx

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8.   
  9. public partial class b : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         string ss = Request.Form["name_c"].ToString() + Request.Form["name_e"].ToString();  
  14.         Response.Write(ss);  
  15.         //ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在  
  16.         写入文本  
  17.         //StreamWriter sr = new StreamWriter(Server.MapPath("test/weather.txt"), false, System.Text.Encoding.Default);  
  18.         //try  
  19.         //{  
  20.         //    sr.Write(Request.Form["name_c"].ToString());  
  21.         //    sr.Close();  
  22.         //    Response.Write("<script>alert('文件写入成功');</script>");  
  23.         //}  
  24.         //catch  
  25.         //{  
  26.         //    Response.Write("<script>alert('文件写入失败');</script>");  
  27.         //}  
  28.     }  
  29.     //检查文件,如果文件不存在则创建  
  30.     private void ExistsFile(string FilePath)  
  31.     {  
  32.         //if(!File.Exists(FilePath))  
  33.         //File.Create(FilePath);  
  34.         //以上写法会报错,详细解释请看下文.........  
  35.         if (!File.Exists(FilePath))  
  36.         {  
  37.             FileStream fs = File.Create(FilePath);  
  38.             fs.Close();  
  39.         }  
  40.     }  
  41.       
  42. }  

转载于:https://www.cnblogs.com/yanergui/p/5014289.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值