在ASP.NET中实现POST发送数据
2024-07-10 12:54:59
供稿:网友
下面的代码实现了与以前xmlhttp类似的功能。代码如下:
httpsenddata.aspx
<%@ page language="<a href="http://dev.21tx.com/dotnet/csharp/" target="_blank">c#</a>"%>
<%@ import namespace = "system"%>
<%@ import namespace = "system.collections"%>
<%@ import namespace = "system.<a href="http://dev.21tx.com/web/" target="_blank">web</a>"%>
<%@ import namespace = "system.web.ui"%>
<%@ import namespace = "system.web.ui.<a href="http://dev.21tx.com/dotnet/aspnet/webcontrols/" target="_blank">webcontrols</a>"%>
<%@ import namespace = "system<a href="http://dev.21tx.com/dotnet/" 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 href="http://dev.21tx.com/web/xml/" 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开发资料库,