在專案中難免會使用 Http POST 方式來傳送資料,之前很白目,用了 PreviousPage 來實作,後來才學會到用 HttpWebRequest 即可直接解決,立馬將所有的程式碼翻一遍,真是對不起,讓它出土太晚...........
Source Page (source.aspx):
protected void Button1_Click(object sender, EventArgs e)
{
string url = "target.aspx";
StringBuilder postData = new StringBuilder();
postData.Append("first_name=" + HttpUtility.UrlEncode(TextBox1.Text) + "&");
postData.Append("last_name=" + HttpUtility.UrlEncode(TextBox2.Text));
// Now to Send Data.
StreamWriter writer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.ToString().Length;
try
{
writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData.ToString());
}
finally
{
if (writer != null)
writer.Close();
}
Response.Redirect("url");
}
Target Page(target.aspx):
..
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string first = Request.Params["first_name"];
string second = Request.Params["last_name"]; <== 收到
}
}
留言
張貼留言