利用Httpwatch实现自动登录
网页分析工具:httpwatch,
开发环境:vs2005 c#
首先,打开网页:http://www2.baidu.com,自动跳转到了http://cas.baidu.com/?tpl=www2& fromu=http%3A%2F%2Fwww2.baidu.com%2F得到它的验证码地址: http://cas.baidu.com/?action=image&key=1252647747466
验证码地址是根据前台js生成的
<script type="text/javascript">
var strValidImgUrl = '/?action=image';
function addTmspan() {
var objImgValid = document.getElementById("imgValid");
var intTm = new Date().getTime();
objImgValid.src = strValidImgUrl + "&key=" + intTm;
}
</script>
提 交的字段:
appid 3
button2 登 录
charset utf-8
entered_imagecode ba27
entered_login aaaaaa
entered_password aaaaaa
fromu http://www2.baidu.com/
登陆成功后,还没完,又经过两次重定位:
http://www2.baidu.com/?castk=878d9sf7f8ea78bd5d515 这个网页 Redirect to http://fengchao.baidu.com/indexAction.do?uid=1806516 (这里变换了域名)
http://fengchao.baidu.com/indexAction.do?uid=1806516 再次转向 Redirect to http://fengchao.baidu.com/indexAction.do?uid=1806516&userid=1806516
最 后才是主页面:http://fengchao.baidu.com/indexAction.do?uid=1806516& userid=1806516
其实,对我们有用的就是 http://www2.baidu.com/?castk=878d9sf7f8ea78bd5d515 这个网页的cookie,有了这个,你才能在登录后做别的操作,否则登陆成功也没有意义。
下面是主程序:
public static string login(out string outcookie,out string out2cookie)
{
string cookie, code, username, userpass;
username = "****";//用户名
userpass = "****";//密码
string posturl = "https://cas.baidu.com/?action=login";
getNumber("http://cas.baidu.com/?action=image&key=" + gettime() + "", "baidu_cas", out cookie, out code);//获取验证码和网页cookie
cookie = cookie.Replace("domain=cas.baidu.com;", "");//整理cookie(对于跨域是必需的)
string poststring = "appid=3&button2=" + System.Web.HttpUtility.UrlEncode("登 录") + "&charset=utf-8&entered_imagecode=" + code + "&entered_login=" + username + "&entered_password=" + userpass + "&fromu=" + System.Web.HttpUtility.UrlEncode("http://www2.baidu.com/") + "";//组织post内容
string refurl = "http://cas.baidu.com/?tpl=www2&fromu=http%3A%2F%2Fwww2.baidu.com%2F";
outcookie = null;
out2cookie = null;
string returnstring = PostPage(posturl, poststring, cookie, refurl, out outcookie);//提交
string geturl = "";
string ret2 = "";
string ret3 = "";
if (outcookie != null)
{
outcookie = outcookie.Replace("domain=cas.baidu.com;", "");//再次整理cookie
geturl = geturlfromstr(returnstring);//获取转向的网址(http://www2.baidu.com/?castk=878d9sf7f8ea78bd5d515)
////继续访问
ret2 = GetHtml(geturl,false , outcookie,out out2cookie );//获得第二个 cookie
out2cookie = out2cookie.Replace("www2", "");//还是要对cookie做整理
}
return ret2;
}
总之,做自动登录就要分析网页,httpwatch是个好东东,仔细研究一下,它能帮我们做很多很多事呢!