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

类别:Asp 点击:0 评论:0 推荐:

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>

 

本文地址:http://com.8s8s.com/it/it8257.htm