关于C#模拟网页登录的问题
我要做一个网站流量统计的工具、数据要通过CNZZ来获取的、但是首先要登录。我想通过模拟提交的方式登录CNZZ然后采集里面的数据,但是登录这块一直弄不好,我下面的代码获取的还是未登录前的源文件。
- C# code
string url = "http://new.cnzz.com/v1/login.php?siteid=******"; string postData = "password=******"; string outdata = ""; string contentType = "application/x-www-form-urlencoded"; string accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, applicat"; string userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET"; private void button1_Click(object sender, EventArgs e) { byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); //request.CookieContainer = cookieContainer; request.Referer = url; request.ContentType = contentType ; request.ContentLength = byteRequest.Length; //附加信息长度 request.Accept = accept; request.UserAgent = userAgent; request.Method = "POST"; Stream streamWrite = request.GetRequestStream(); streamWrite.Write(byteRequest, 0, byteRequest.Length); streamWrite.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamRead = response.GetResponseStream(); //转换为数据流 StreamReader reader = new StreamReader(streamRead, Encoding.GetEncoding("gb2312")); string html=reader.ReadToEnd(); //通过StreamReader类读取流 reader.Close(); streamRead.Close();
上面我通过抓包过来的postData为什么只有password 一项的、其他什么都没有、小小实习生求指教啊、急
[解决办法]
请求成功后保存该对象 HttpWebRequest.CookieContainer
下次请求开始时 设置该对象