自動登錄百度推廣後臺

本文轉載自: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生成的
9310e85a14af99de4811ff4c77f1f911.jpe24a924a57ba6b3f2b51fc9edb7ea4186.jpejCode
<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是個難點,相當於換域了)
提交的字段:
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,有了這個,你才能在登錄後做別的操作,否則登陸成功也沒有意義。

下面是主程序:
9310e85a14af99de4811ff4c77f1f911.jpe24a924a57ba6b3f2b51fc9edb7ea4186.jpeCode
    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,不要放過任何一個細節
發佈了0 篇原創文章 · 獲贊 130 · 訪問量 77萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章