C语言版 博客园用户登陆
1 #include <stdio.h> 2 #include <curl/curl.h> 3 4 #define LOGIN_URL "http://m.cnblogs.com/mobileLoginPost.aspx" 5 #define USER_NAME "myname" 6 #define PASSWORD "mypassword" 7 8 FILE *fp; 9 size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream) 10 { 11 int written = fwrite (ptr, size, nmemb, (FILE *) fp); 12 return written; 13 } 14 15 int main(void) 16 { 17 fp = fopen("html.txt", "w"); 18 if ( fp == NULL ) 19 { 20 printf("open file failed.\n"); 21 } 22 CURL *curl; 23 CURLcode res; 24 struct curl_slist *headerlist = NULL; 25 curl = curl_easy_init(); 26 if (curl) 27 { 28 char nline[256]; 29 snprintf(nline, sizeof(nline), "tbUserName=%s&tbPassword=%s&chkRemember=on&txtReturnUrl=", USER_NAME, PASSWORD); 30 curl_easy_setopt(curl, CURLOPT_URL, LOGIN_URL); 31 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); 32 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 33 curl_easy_setopt(curl, CURLOPT_POST, 1); 34 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 35 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 36 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie_login.txt"); 37 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_login.txt"); 38 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, nline); 39 40 res = curl_easy_perform(curl); 41 42 if (res != CURLE_OK) 43 fprintf(stderr, "curl_easy_perform() failed: %s\n", 44 curl_easy_strerror(res)); 45 46 curl_easy_cleanup(curl); 47 curl_slist_free_all(headerlist); 48 } 49 return 0; 50 }
啊 要睡觉了,很晚了 明天试试闪存获取和发闪存。