.net 后台提交表单

来源:https://blog.csdn.net/wem520/article/details/22278543

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

<%@ 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>

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();
}
}
}
}

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();
}
}

}

猜你喜欢

转载自www.cnblogs.com/guitarrock/p/9185819.html
今日推荐