.net 后台提交表单,获取返回结果
2015-11-09 19:27
489 查看
a.aspx后台提交表单,b.aspx接收表单(根据input的name获得值)
1、a.aspx
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="name" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
后台代码
[csharp] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class a : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//如果表单中要发送中文,可以对数据进行编码gb2312/gbk。
Encoding myencode = Encoding.GetEncoding("gb2312");
//然后处理要传的表单数据。
string strpost = HttpUtility.UrlEncode("name_c", myencode) + "=" + HttpUtility.UrlEncode(name.Text, myencode)+"&"
+ HttpUtility.UrlEncode("name_e", myencode) + "=" + HttpUtility.UrlEncode(name.Text+"测试", myencode);
//string strpost = "name_c=" + name.Text + "&" + "name_e=" + name.Text;
//有多个参数可以用"&"拼接。
//接着序列化参数。
byte[] postBytes = Encoding.ASCII.GetBytes(strpost);
//创建请求示例。
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:61444/b.aspx");
//下面可以选择请求的方式,标头。
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
using (Stream sendStream = req.GetRequestStream())
{
sendStream.Write(postBytes, 0, Convert.ToInt32(req.ContentLength));
}
using(WebResponse wr=req.GetResponse())
{
Stream respStream = wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding("utf-8")))
{
Label1.Text = reader.ReadToEnd();
}
}
}
}
2、b.aspx
[csharp] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class b : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ss = Request.Form["name_c"].ToString() + Request.Form["name_e"].ToString();
Response.Write(ss);
//ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
////写入文本
//StreamWriter sr = new StreamWriter(Server.MapPath("test/weather.txt"), false, System.Text.Encoding.Default);
//try
//{
// sr.Write(Request.Form["name_c"].ToString());
// sr.Close();
// Response.Write("<script>alert('文件写入成功');</script>");
//}
//catch
//{
// Response.Write("<script>alert('文件写入失败');</script>");
//}
}
//检查文件,如果文件不存在则创建
private void ExistsFile(string FilePath)
{
//if(!File.Exists(FilePath))
//File.Create(FilePath);
//以上写法会报错,详细解释请看下文.........
if (!File.Exists(FilePath))
{
FileStream fs = File.Create(FilePath);
fs.Close();
}
}
}
1、a.aspx
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="name" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
后台代码
[csharp] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class a : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//如果表单中要发送中文,可以对数据进行编码gb2312/gbk。
Encoding myencode = Encoding.GetEncoding("gb2312");
//然后处理要传的表单数据。
string strpost = HttpUtility.UrlEncode("name_c", myencode) + "=" + HttpUtility.UrlEncode(name.Text, myencode)+"&"
+ HttpUtility.UrlEncode("name_e", myencode) + "=" + HttpUtility.UrlEncode(name.Text+"测试", myencode);
//string strpost = "name_c=" + name.Text + "&" + "name_e=" + name.Text;
//有多个参数可以用"&"拼接。
//接着序列化参数。
byte[] postBytes = Encoding.ASCII.GetBytes(strpost);
//创建请求示例。
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:61444/b.aspx");
//下面可以选择请求的方式,标头。
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
using (Stream sendStream = req.GetRequestStream())
{
sendStream.Write(postBytes, 0, Convert.ToInt32(req.ContentLength));
}
using(WebResponse wr=req.GetResponse())
{
Stream respStream = wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding("utf-8")))
{
Label1.Text = reader.ReadToEnd();
}
}
}
}
2、b.aspx
[csharp] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class b : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ss = Request.Form["name_c"].ToString() + Request.Form["name_e"].ToString();
Response.Write(ss);
//ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
////写入文本
//StreamWriter sr = new StreamWriter(Server.MapPath("test/weather.txt"), false, System.Text.Encoding.Default);
//try
//{
// sr.Write(Request.Form["name_c"].ToString());
// sr.Close();
// Response.Write("<script>alert('文件写入成功');</script>");
//}
//catch
//{
// Response.Write("<script>alert('文件写入失败');</script>");
//}
}
//检查文件,如果文件不存在则创建
private void ExistsFile(string FilePath)
{
//if(!File.Exists(FilePath))
//File.Create(FilePath);
//以上写法会报错,详细解释请看下文.........
if (!File.Exists(FilePath))
{
FileStream fs = File.Create(FilePath);
fs.Close();
}
}
}
相关文章推荐
- GCD中的线程死锁问题
- .net 后台提交表单,获取返回结果
- AutoCompleteTextView 自动完成输入内容对的控件
- LDD: Ch 3 字符设备驱动程序
- uva10250
- 关于Intellij IDEA菜单项中Compile、Make和Build的区别
- 抽象工程的模式实例
- Codeforces Round #330 (Div. 1) B. Max and Bike
- 红黑树的性质及旋转
- HDU 4288 Coder(线段树)
- How Did Watson Answer? —— About Question Analysis
- 【NOIP2015】运输计划
- php连接mysql数据库的几种方法
- tomcat目录结构、web应用的目录结构及相关应用
- 1002. A+B for Polynomials (25)
- UIView 杂记
- OSG的CompositeViewer和Viewer比较
- 命令行设置环境变量
- Hadoop辅助排序样例一
- POJ 2318 TOYS || POJ 2398 Toy Storage