创建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");