ASP.NET 抓取网页内容-Post 数据
在抓取网页时,有时候,需要将某些数据通过 Post 的方式发送到服务器,将以下代码添加在网页抓取的程序中,以实现将用户名和密码 Post 到服务器:
string data = "userName=admin&passwd=admin888";
byte[] requestBuffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(data);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestBuffer.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(requestBuffer, 0, requestBuffer.Length);
requestStream.Close();
}
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")))
{
string str = reader.ReadToEnd();
reader.Close();
}
byte[] requestBuffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(data);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestBuffer.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(requestBuffer, 0, requestBuffer.Length);
requestStream.Close();
}
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")))
{
string str = reader.ReadToEnd();
reader.Close();
}
以上是以编码 gb2312 为例。
/************************************************/
本博客内容如果是原著都会在标题后加上(原著)字样,未加者多数为转载.
/************************************************/