本文转载自:
https://www.cnblogs.com/fatlity/archive/2009/09/11/1564754.html 作者:fatlity 转载请注明该声明。
这是前几天做的一个小项目,之所以拿它做例子是因为它很复杂。
网页分析工具: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生成的
提交的字段:
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,有了这个,你才能在登录后做别的操作,否则登陆成功也没有意义。
下面是主程序:
总之,做自动登录就要分析网页,熟练运用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生成的
jCode
<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>
然后,取得提交地址:https://cas.baidu.com/?action=login (注意:https是个难点,相当于换域了)
<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,有了这个,你才能在登录后做别的操作,否则登陆成功也没有意义。
下面是主程序:
Code
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;
}
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,不要放过任何一个细节