在ASP.NET中實現POST發送數據
2024-07-10 12:54:59
供稿:網友
下面的代碼實現了與以前xmlhttp類似的功能。代碼如下:
httpsenddata.aspx
<%@ page language="<a target="_blank">c#</a>"%>
<%@ import namespace = "system"%>
<%@ import namespace = "system.collections"%>
<%@ import namespace = "system.<a target="_blank">web</a>"%>
<%@ import namespace = "system.web.ui"%>
<%@ import namespace = "system.web.ui.<a target="_blank">webcontrols</a>"%>
<%@ import namespace = "system<a target="_blank">.net</a>"%>
<%@ 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://<a target="_blank">xml</a>.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=" 發 送 " ></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>注冊會員,創建你的web開發資料庫,