libcurl使用心得
Libcurl為一個免費開源的,客戶端url傳輸庫,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,線程安全,支持Ipv6。並且易於使用。
從http://curl.haxx.se/libcurl/ 下載一個穩定的版本,注意選擇OS。
在使用之前請大家多閱讀libcurl的文檔:因為如果要實際運用到項目中,最好對libcurl有具體的了解,具體在
http://curl.haxx.se/libcurl/c/
curl_easy_setopt()
curl_easy_perform()
curl_easy_getinfo()
這三個函數的使用上,需要多去鑽研,多看Samples,你才能靈活使用libcurl。
感謝這篇文章:
http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/
給了我許多啟發,再次感謝!
給出我的一個簡單的代碼例子:
說明:
1.關鍵在curl_easy_setopt函數設置option,可以設置ftp,http,get,post等許多選項,請根據具體使用情況設置。
2.對取回來的數據需要進行判斷,比如http下載文件,如果文件不存在,需要進行處理。因為writer是可以將buf填充404 not found等網頁內容的,不能將這個內容當成文件內容,所以需要判斷http web返回來的code,進行判斷。
3.我有個問題,就是想得到服務器上filename的具體名稱,verbose調試已經返回了,但是我在getinfo的時候,試過好多選項,但未找到這個存放真實服務器文件名的選項,如果有知道的麻煩告訴我,謝謝了!
#include "curl/curl.h" #pragma comment(lib, "libcurl.lib") long writer(void *data, int size, int nmemb, string &content); bool CurlInit(CURL *&curl, const char* url,string &content); bool GetURLDataBycurl(const char* URL, string &content); void main() { char *url ="http://www.baidu.com/img/baidu.gif"; string content; if ( GetURLDataBycurl(url,content)) { printf("%s\n",content); } getchar(); } bool CurlInit(CURL *&curl, const char* url,string &content) { CURLcode code; string error; curl = curl_easy_init(); if (curl == NULL) { printf( "Failed to create CURL connection\n"); return false; } code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error); if (code != CURLE_OK) { printf( "Failed to set error buffer [%d]\n", code ); return false; } curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); code = curl_easy_setopt(curl, CURLOPT_URL, url); if (code != CURLE_OK) { printf("Failed to set URL [%s]\n", error); return false; } code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); if (code != CURLE_OK) { printf( "Failed to set redirect option [%s]\n", error ); return false; } code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); if (code != CURLE_OK) { printf( "Failed to set writer [%s]\n", error); return false; } code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); if (code != CURLE_OK) { printf( "Failed to set write data [%s]\n", error ); return false; } return true; } long writer(void *data, int size, int nmemb, string &content) { long sizes = size * nmemb; string temp(data,sizes); content += temp; return sizes; } bool GetURLDataBycurl(const char* URL, string &content) { CURL *curl = NULL; CURLcode code; string error; code = curl_global_init(CURL_GLOBAL_DEFAULT); if (code != CURLE_OK) { printf( "Failed to global init default [%d]\n", code ); return false; } if ( !CurlInit(curl,URL,content) ) { printf( "Failed to global init default [%d]\n" ); return PM_FALSE; } code = curl_easy_perform(curl); if (code != CURLE_OK) { printf( "Failed to get '%s' [%s]\n", URL, error); return false; } long retcode = 0; code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode); if ( (code == CURLE_OK) && retcode == 200 ) { double length = 0; code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length); printf("%d",retcode); FILE * file = fopen("1.gif","wb"); fseek(file,0,SEEK_SET); fwrite(content.c_str(),1,length,file); fclose(file); //struct curl_slist *list; //code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list); //curl_slist_free_all (list); return true; } else { // debug1( "%s \n ",getStatusCode(retcode)); return false; } curl_easy_cleanup(curl); return false; }
原地址:http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html