在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路: 观察这三个请求的详细信息,不难看出第一个GET请求的地址可以由POST的响应得到,而第二个GET请求的地址又由第一个GET的响应得到。
HttpWebRequest request = null; HttpWebResponse response = null; string gethost = string.Empty; CookieContainer cc = new CookieContainer(); string Cookiesstr = string.Empty; try { //第一次POST请求 string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化 string LoginUrl="http://www.renren.com/PLogin.do"; request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类 request.Method = "POST";//数据提交方式为POST //模拟头 request.ContentType = "application/x-www-form-urlencoded"; byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata); request.ContentLength = postdatabytes.Length; //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl; request.AllowAutoRedirect = false; request.CookieContainer = cc; request.KeepAlive = true; //提交请求 Stream stream; stream = request.GetRequestStream(); stream.Write(postdatabytes, 0, postdatabytes.Length); stream.Close(); //接收响应 response = (HttpWebResponse)request.GetResponse(); //保存返回cookie response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); CookieCollection cook = response.Cookies; string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri); Cookiesstr = strcrook; //取第一次GET跳转地址 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string content = sr.ReadToEnd(); response.Close(); string[] substr = content.Split(new char[] { '"' }); gethost = substr[1]; } catch (Exception) { //第一次POST出错; } 注释写的很详细了,在这就不再分析,也许有人对request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑问,可以去google一下HttpWebRequest和WebRequest的区别,简单来说WebRequest是一个抽象类,不能直接实例化,需要被继承,而HttpWebRequest继承自WebRequest。 再模拟第一个和第二个GET请求
try { request = (HttpWebRequest)WebRequest.Create(gethost); request.Method = "GET"; request.KeepAlive = true; request.Headers.Add("Cookie:" + Cookiesstr); request.CookieContainer = cc; request.AllowAutoRedirect = false; response = (HttpWebResponse)request.GetResponse(); //设置cookie Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); //取再次跳转链接 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string ss = sr.ReadToEnd(); string[] substr = ss.Split(new char[] { '"' }); gethost = substr[1]; request.Abort(); sr.Close(); response.Close(); } catch (Exception) { //第一次GET出错 } try { //第二次GET请求 request = (HttpWebRequest)WebRequest.Create(gethost); request.Method = "GET"; request.KeepAlive = true; request.Headers.Add("Cookie:" + Cookiesstr); request.CookieContainer = cc; request.AllowAutoRedirect = false; response = (HttpWebResponse)request.GetResponse(); //设置cookie Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); request.Abort(); response.Close(); } catch (Exception) { //第二次GET出错 } GET与POST请求大同小异,这里便不再累述。三次请求结束,保存好你的cookie string,每次请求的时候都赋给请求的头部,你就处于登录状态了。 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论