zoukankan      html  css  js  c++  java
  • .net 后台提交表单,获取返回结果

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

    1、a.aspx

    [html] view plaincopy在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 plaincopy在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 plaincopy在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. }  
  • 相关阅读:
    ava的动态代理机制详解
    Windows10系统切换JDK版本(前提是装了多个版本的JDK)
    关于springMVC传参问题
    js的replace函数把"$"替换成成"$"
    vue框架之组件通信及插槽简介等相关内容-113
    vue框架之前后端交互等相关内容-112
    django框架之cookie和session的使用等相关内容-74
    vue框架之基础语法等相关内容-111
    vue框架之初始、模板语法、指令等相关内容-110
    django框架之forms渲染错误信息、全局钩子及cookie引入等相关内容-73
  • 原文地址:https://www.cnblogs.com/yanergui/p/5014289.html
Copyright © 2011-2022 走看看