技术文章 > 在ASP.NET中实现POST发送数据

在ASP.NET中实现POST发送数据

2017-12-12 04:40

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

下面的代码实现了与以前XMLHTTP类似的功能。代码如下:
HttpSendData.aspx
<%@ Page language=“c#“%>
<%@ Import Namespace = “System“%>
<%@ Import Namespace = “System.Collections“%>
<%@ Import Namespace = “System.Web“%>
<%@ Import Namespace = “System.Web.UI“%>
<%@ Import Namespace = “System.Web.UI.WebControls“%>
<%@ Import Namespace = “System.Net“%>
<%@ Import Namespace = “System.IO“%>
<%@ Import Namespace = “System.Text“%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN“ >
<HTML>
<HEAD>
<script runat=“server“>
void Button1_Click(object sender, System.EventArgs e)
{
string strTitle = TextBox1.Text;
string strDesc = TextBox2.Text;
Encoding encoding = Encoding.GetEncoding(“GB2312“);
string postData = “Title=“ + strTitle;
string strUrl = “http://xml.sz.luohuedu.net/HttpReceiveData.<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>x“;
postData += (“&Desc=“ + strDesc);
byte[] data = encoding.GetBytes(postData);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = “POST“;
myRequest.ContentType=“application/x-www-form-urlencoded“;
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// 发送数据
newStream.Write(data,0,data.Length);
newStream.Close();
Response.Redirect(“HttpSendData.<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>x“);
}
</script>
</HEAD>
<body>
<form id=“HTTPPost“ method=“post“ runat=“server“>
标题:<<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:TextBox id=“TextBox1“ runat=“server“></<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:TextBox>
<br>
内容:
<br>
<<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:TextBox id=“TextBox2“ runat=“server“ TextMode=“MultiLine“ Rows=“10“ Columns=“100“></<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:TextBox>
<br>
<<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:Button id=“Button1“ runat=“server“ Text=“ 发 送 “ onclick=“Button1_Click“></<a href=“http://www.chinaitpower.com/Dev/Web/Asp/index.html“ target=“_blank“>asp</a>:Button>
</form>
</body>
</HTML>
HttpReceiveData.aspx
<%@ Page Language=“vb“%>
<%@ import Namespace = “System“ %>
<%@ import Namespace = “System.Web.UI“ %>
<%@ import Namespace = “System.Web“ %>
<script runat=“server“>
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Request.ServerVariables(“REQUEST_METHOD“).ToString() = “POST“ Then
Dim connstr As String
connstr = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“ + Server.MapPath(“Test.mdb“)
Dim cn As New System.Data.OleDb.OleDbConnection(connstr)
Dim strSQL As String = “INSERT INTO TestTable (Title,Description) VALUES(““ _
+ Request.Form(“Title“).ToString() + ““,““ + Request.Form(“Desc“).ToString() + ““)“
cn.Open()
Dim cmd As New System.Data.OleDb.OleDbCommand(strSQL, cn)
cmd.ExecuteNonQuery()
cn.Close()
cn.Dispose()
cmd.Dispose()
End If
End Sub
</script>