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

 41 return   0 ;
 42 }

 43
 44 /* *******************login_hi_baidu**************** */
 45 #include  < stdio.h >
 46 #include  < string .h >
 47 #include  < curl / curl.h >
 48 #include  < curl / types.h >
 49 #include  < curl / easy.h >
 50
 51 int  main( int  argc,  char   * argv[])
 52 {
 53 CURL  * curl;
 54 CURLcode res;
 55
 56 struct  curl_slist  * headerlist = NULL;
 57 static   const   char  buf[]  =   " Expect: " ;
 58
 59 curl_global_init(CURL_GLOBAL_ALL);
 60 curl  =  curl_easy_init();
 61 headerlist  =  curl_slist_append(headerlist, buf);
 62
 63 if (curl) 
 64 {
 65     curl_easy_setopt(curl, CURLOPT_URL,  " https://passport.baidu.com/?login " );
 66     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
 67
 68     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
 70     curl_easy_setopt(curl,CURLOPT_COOKIEFILE, " cookie_open.txt " ); // 提交第一步保存的cookie
 71     curl_easy_setopt(curl,CURLOPT_COOKIEJAR, " cookie_login.txt " ); // 保存登陆后的cookie
 72 #ifdef SKIP_PEER_VERIFICATION
 73     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,  0L );
 74 #endif
 75
 76 #ifdef SKIP_HOSTNAME_VERFICATION
 77     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,  0L );
 78 #endif
 79
 80     res  =  curl_easy_perform(curl);
 81      /*  always cleanup  */
 82     curl_easy_cleanup(curl);
 83      /*  free slist  */
 84     curl_slist_free_all(headerlist);
 85 }

 86 return   0 ;
 87 }

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

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

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

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

173 return   0 ;
174 }

175
176
177
178
179
180