標籤:blog http io os ar for sp 資料 div
設計思路
建立HTTPWebRequest類的一個執行個體,設定這個對象的Method屬性為"POST",ContentType屬性為"application/x-/www-form-urlencoded"。通過GetRequestStream()方法和Stream.Write()為要發起的請求添加POST資料。然後通過HttpWebRequest.GetResponse()方法取回相應的HTTP響應。
例子:
//用自己製作的網頁都可以,這裡用百度做例子。string url="http://www.baidu.com"; string data ="inputBox1=orange";byte[] buffer = Encoding.ASCII.GetBytes(data);HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);req.Method="POST";req.ContenType="application/x-www-form-urlencoded";req.ContenLength=buffer.Length;Stream reqst = req.GetRequestStream();reqst.Write(buffer,0,buffer.Length);reqst.Flush();reqst.Close();Console.WriteLine("\nGrabbing‘orange‘");HttpWebResponse res = (HttpWebResponse)req.GetResponse();Stream resst = res.GetResponseStream();StreamReader sr = new StreamReader(resst);Console.WriteLine("\nGrabbing HTTP response\n");Console.WtiteLine(sr.ReadToEnd());sr.Close();resst.Close();Console.WriteLine("Done");
C#發送簡單的HTTP POST請求給傳統的ASP網頁。