原文来自:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/06/2278122.html(感谢博友的分享)
模拟登陆有一下几个步骤
第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest;
第二 模拟POST或者GET方式提交的数据;
第三 模拟请求的头;
第四 提交请求并获得响应,及对响应做我们所需要的处理。
这里我们以人人网的登录为例,将涉及到POST以及GET两种请求方式。
①抓包
用firefox的firebug、httpwatch都可以,这里用的是fiddler。登录人人网的时候用fiddler抓包,如下图:
找到红色箭头指向的那个POST包,后面还跟了两个GET包。
②模拟登录
POST登录:
public String getContent()
{
HttpWebRequest request = null;
HttpWebResponse response = null;
string gethost = string.Empty;
string content="";
CookieContainer cc = new CookieContainer(); //若要从远程调用中获取COOKIE一定要为request设定一个CookieContainer用来装载返回的cookies
string Cookiesstr = string.Empty;
try
{
//第一次POST请求
string UserName = "XXXXX";
string PassWord = "XXXXX";
string HostUrl = "http://guide.renren.com/guide";
string postdata = "email=" + UserName.Replace("@", "@") +"&password=" + PassWord +"&origURL=" + HostUrl +"&domain=renren.com";//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“@”,所以我们也要作此变化
//string LoginUrl = "http://www.renren.com/PLogin.do";//这个POST包没抓到过,估计是没有了,抓到的是下面这个
string LoginUrl = "http://www.renren.com/ajaxLogin";
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);
content = sr.ReadToEnd();
response.Close();
//string[] substr = content.Split(new char[] { '"' });
//gethost = substr[1];
}
catch (Exception)
{
MessageBox.Show("error");
}
return content;
}
模拟GET登录:
GET与POST请求大同小异,三次请求结束,保存好你的cookiestring,每次请求的时候都赋给请求的头部,你就处于登录状态了。
在上面的POST登录中获得的gethost和cookiesstr保存起来,用到下面的程序里就可以实现固定帐号的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();
MessageBox.Show("1GET" + ss);
string[] substr = ss.Split(new char[] { '"' });
gethost = substr[1];
request.Abort();
sr.Close();
response.Close();
}
catch (Exception)
{
//第一次GET出错
}