百度自動登陸代碼實現

 

 

  1 ExpandedBlockStart.gif /* ************open_login_page.c********** */
  2 None.gif#include  < stdio.h >
  3 None.gif#include  < string .h >
  4 None.gif#include  < curl / curl.h >
  5 None.gif#include  < curl / types.h >
  6 None.gif#include  < curl / easy.h >
  7 None.gif
  8 None.gif int  main( int  argc,  char   * argv[])
  9 ExpandedBlockStart.gif {
 10 InBlock.gifCURL  * curl;
 11 InBlock.gifCURLcode res;
 12 InBlock.gif
 13 InBlock.gif struct  curl_slist  * headerlist = NULL;
 14 InBlock.gif static   const   char  buf[]  =   " Expect: " ;
 15 InBlock.gif
 16 InBlock.gifcurl_global_init(CURL_GLOBAL_ALL);
 17 InBlock.gifcurl  =  curl_easy_init();
 18 InBlock.gifheaderlist  =  curl_slist_append(headerlist, buf);
 19 InBlock.gif
 20 InBlock.gif if (curl) 
 21 ExpandedSubBlockStart.gif {
 22 InBlock.gif    curl_easy_setopt(curl, CURLOPT_URL,  " https://passport.baidu.com/?login " );
 23 InBlock.gif    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
 24 InBlock.gif    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, " cookie_open.txt " ); // 把服務器發過來的cookie保存到cookie_open.txt
 25 InBlock.gif
 26 InBlock.gif#ifdef SKIP_PEER_VERIFICATION
 27 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,  0L );
 28 InBlock.gif #endif
 29 InBlock.gif
 30 InBlock.gif#ifdef SKIP_HOSTNAME_VERFICATION
 31 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,  0L );
 32 InBlock.gif #endif
 33 InBlock.gif    res  =  curl_easy_perform(curl);
 34 InBlock.gif
 35 ExpandedSubBlockStart.gif     /*  always cleanup  */
 36 InBlock.gif    curl_easy_cleanup(curl);
 37 InBlock.gif
 38 ExpandedSubBlockStart.gif     /*  free slist  */
 39 InBlock.gif    curl_slist_free_all(headerlist);
 40 ExpandedSubBlockEnd.gif}

 41 InBlock.gif return   0 ;
 42 ExpandedBlockEnd.gif}

 43 None.gif
 44 ExpandedBlockStart.gif /* *******************login_hi_baidu**************** */
 45 None.gif#include  < stdio.h >
 46 None.gif#include  < string .h >
 47 None.gif#include  < curl / curl.h >
 48 None.gif#include  < curl / types.h >
 49 None.gif#include  < curl / easy.h >
 50 None.gif
 51 None.gif int  main( int  argc,  char   * argv[])
 52 ExpandedBlockStart.gif {
 53 InBlock.gifCURL  * curl;
 54 InBlock.gifCURLcode res;
 55 InBlock.gif
 56 InBlock.gif struct  curl_slist  * headerlist = NULL;
 57 InBlock.gif static   const   char  buf[]  =   " Expect: " ;
 58 InBlock.gif
 59 InBlock.gifcurl_global_init(CURL_GLOBAL_ALL);
 60 InBlock.gifcurl  =  curl_easy_init();
 61 InBlock.gifheaderlist  =  curl_slist_append(headerlist, buf);
 62 InBlock.gif
 63 InBlock.gif if (curl) 
 64 ExpandedSubBlockStart.gif {
 65 InBlock.gif    curl_easy_setopt(curl, CURLOPT_URL,  " https://passport.baidu.com/?login " );
 66 InBlock.gif    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
 67 InBlock.gif
 68 InBlock.gif    curl_easy_setopt(curl,CURLOPT_POSTFIELDS, " username=ea99&password=&psp_tt=0&safeflg=0&return_method=get&u=http://hi.baidu.com/s%5Fyqguo " ); // 設置帳號密碼,其餘的信息是頁面要求的,抓包即可看見。
 69 InBlock.gif
 70 InBlock.gif    curl_easy_setopt(curl,CURLOPT_COOKIEFILE, " cookie_open.txt " ); // 提交第一步保存的cookie
 71 InBlock.gif     curl_easy_setopt(curl,CURLOPT_COOKIEJAR, " cookie_login.txt " ); // 保存登陸後的cookie
 72 InBlock.gif #ifdef SKIP_PEER_VERIFICATION
 73 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,  0L );
 74 InBlock.gif #endif
 75 InBlock.gif
 76 InBlock.gif#ifdef SKIP_HOSTNAME_VERFICATION
 77 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,  0L );
 78 InBlock.gif #endif
 79 InBlock.gif
 80 InBlock.gif    res  =  curl_easy_perform(curl);
 81 ExpandedSubBlockStart.gif     /*  always cleanup  */
 82 InBlock.gif    curl_easy_cleanup(curl);
 83 ExpandedSubBlockStart.gif     /*  free slist  */
 84 InBlock.gif    curl_slist_free_all(headerlist);
 85 ExpandedSubBlockEnd.gif}

 86 InBlock.gif return   0 ;
 87 ExpandedBlockEnd.gif}

 88 None.gif
 89 None.gif
 90 ExpandedBlockStart.gif /* ***********download_private_page.c***************** */
 91 None.gif#include  < stdio.h >
 92 None.gif#include  < string .h >
 93 None.gif#include  < curl / curl.h >
 94 None.gif#include  < curl / types.h >
 95 None.gif#include  < curl / easy.h >
 96 None.gif
 97 None.gif static  size_t write_data( void   * ptr, size_t size, size_t nmemb,  void   * stream)
 98 ExpandedBlockStart.gif {
 99 InBlock.gif    int  written  =  fwrite(ptr, size, nmemb, (FILE  * )stream);
100 InBlock.gif    return  written;
101 ExpandedBlockEnd.gif}

102 None.gif
103 None.gif int  main( int  argc,  char   * argv[])
104 ExpandedBlockStart.gif {
105 InBlock.gifCURL  * curl;
106 InBlock.gifCURLcode res;
107 InBlock.gif
108 InBlock.gif static   const   char   * headerfilename  =   " head.out " ;
109 InBlock.gifFILE  * headerfile;
110 InBlock.gif
111 InBlock.gif static   const   char   * bodyfilename  =   " body.html " ;
112 InBlock.gifFILE  * bodyfile;
113 InBlock.gif
114 InBlock.gif struct  curl_slist  * headerlist = NULL;
115 InBlock.gif static   const   char  buf[]  =   " Expect: " ;
116 InBlock.gif
117 InBlock.gifcurl_global_init(CURL_GLOBAL_ALL);
118 InBlock.gifcurl  =  curl_easy_init();
119 InBlock.gifheaderlist  =  curl_slist_append(headerlist, buf);
120 InBlock.gif if (curl) 
121 ExpandedSubBlockStart.gif {
122 InBlock.gif     // 拉取私有頁面
123 InBlock.gif     curl_easy_setopt(curl, CURLOPT_URL,  " http://hi.baidu.com/ea99/blog/item/c4e99e58d7ec9d86800a18a2.html " );
124 InBlock.gif    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
125 InBlock.gif
126 InBlock.gif    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
127 InBlock.gif
128 InBlock.gif    headerfile  =  fopen(headerfilename, " w " );
129 InBlock.gif     if  (headerfile  ==  NULL) 
130 ExpandedSubBlockStart.gif     {
131 InBlock.gif      curl_easy_cleanup(curl);
132 InBlock.gif      curl_global_cleanup();
133 InBlock.gif      curl_slist_free_all(headerlist);
134 InBlock.gif      printf( " open head.out file failed!/n " );
135 InBlock.gif       return   - 1 ;
136 ExpandedSubBlockEnd.gif    }

137 InBlock.gif
138 InBlock.gif    bodyfile  =  fopen(bodyfilename, " w " );
139 InBlock.gif     if  (bodyfile  ==  NULL) 
140 ExpandedSubBlockStart.gif     {
141 InBlock.gif      curl_easy_cleanup(curl);
142 InBlock.gif      curl_global_cleanup();
143 InBlock.gif      curl_slist_free_all(headerlist);
144 InBlock.gif      printf( " open body.html file failed!/n " );
145 InBlock.gif       return   - 1 ;
146 ExpandedSubBlockEnd.gif    }

147 InBlock.gif
148 InBlock.gif    curl_easy_setopt(curl,CURLOPT_WRITEHEADER, headerfile);
149 InBlock.gif    curl_easy_setopt(curl,CURLOPT_WRITEDATA,bodyfile);
150 InBlock.gif
151 InBlock.gif    curl_easy_setopt(curl,CURLOPT_COOKIEFILE, " cookie_login.txt " ); // 把第二步保存的cookie發送給服務器驗證
152 InBlock.gif
153 InBlock.gif#ifdef SKIP_PEER_VERIFICATION
154 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,  0L );
155 InBlock.gif #endif
156 InBlock.gif
157 InBlock.gif#ifdef SKIP_HOSTNAME_VERFICATION
158 InBlock.gif    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,  0L );
159 InBlock.gif #endif
160 InBlock.gif    res  =  curl_easy_perform(curl);
161 ExpandedSubBlockStart.gif     /*  always cleanup  */
162 InBlock.gif    curl_easy_cleanup(curl);
163 ExpandedSubBlockStart.gif     /*  free slist  */
164 InBlock.gif    curl_slist_free_all(headerlist);
165 InBlock.gif    curl_global_cleanup();
166 InBlock.gif     if (headerfile  !=  NULL)
167 InBlock.gif   fclose(headerfile);
168 InBlock.gif
169 InBlock.gif     if (bodyfile  !=  NULL)
170 InBlock.gif       fclose(bodyfile);
171 InBlock.gif
172 ExpandedSubBlockEnd.gif}

173 InBlock.gif return   0 ;
174 ExpandedBlockEnd.gif}

175 None.gif
176 None.gif
177 None.gif
178 None.gif
179 None.gif
180 None.gif

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章