跳到主要內容

發表文章

目前顯示的是 8月, 2016的文章

利用 HttpWebRequest 來 POST Data

在專案中難免會使用 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.GetR