CSDN博客

img xchonglfm

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

发表于2004/7/9 9:02:00  670人阅读

分类: ASP.NET

下面的代码实现了与以前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.aspx"; 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.aspx"); } </script> </head> <body> <form id="HTTPPost" method="post" runat="server"> 标题:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <br /> 内容: <br /> <asp:TextBox id="TextBox2" runat="server" TextMode="MultiLine" Rows="10" Columns="100"></asp:TextBox> <br /> <asp:Button id="Button1" runat="server" Text=" 发 送 " onclick="Button1_Click"></asp: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>
0 0

相关博文

我的热门文章

img
取 消
img