Post问题求助,如何将A页面post到B的值在B页片面的控件中显示。
Post问题求助,如何将A页面post到B的值在B页片面的控件中显示。
我网上抄了段代码:
A页 post
string postData = "uUsername=abc&uPassword=ccc";
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData);
string postUrl = "http://localhost:5880/WebSitePostTest/B.aspx";
// 准备请求...
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
try
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
// 发送数据
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}
rep.Close();
readStream.Close();
Response.Write(sb.ToString());
Response.Write("Login success!");
return true;
}
catch (Exception ex)
{
Response.Write("Login error:" + ex.Message.ToString());
return false;
}
Response.Redirect("http://localhost:5880/WebSitePostTest/B.aspx");
B页 Page_Load(object sender, EventArgs e)接收
string aa = Request.ServerVariables["REQUEST_METHOD"];
if (aa.Trim().ToUpper() == "POST")
{
if (Request.Form["uPassword"] != null && Request.Form["uUsername"] != "")
{
string uUsername = Request.Form["uUsername"].ToString();
string uPassword = Request.Form["uPassword"].ToString();
this.TextBox1.Text = uUsername+uPassword;
}
}
问题: this.TextBox1.Text 显示时为空。
调过调试发现,B页Page_Load方法会运行两次。
不解。