由於項目的需要,需要使用libcurl庫進行開發,如果只是簡單的利用libcurl來獲取html的源碼,則不需要花費很多力氣去研究libcurl.
libcurl庫是很強大的。有空可以慢慢研究哦!
使用libcurl庫的流程如下:
1、首先是導入libcurl庫
2、curl_easy_init(); //初始化
3、curl_easy_setopt(); //設置選項
4、curl_easy_perform(curl); //執行curl
5、curl_easy_cleanup(curl); //清除curl資源
代碼中貼出了兩個接收數據的不同處理方式,大同小異!if(true)else....
#include <stdio.h> #include <> #include comment(lib,"libcurl.lib") comment(lib,"libcurld.lib") comment(lib,"libcurl_imp.lib") comment(lib,"libcurld_imp.lib") size_t GetContent( *buffer, size_t size, size_t nmemb, * size_t FuncCallBack( *data, size_t size, size_t nmemb, * GetHtmlFromUrl( * GetHtmlFromUrl( GetHtmlFromUrl( * CURL * errbuf[CURL_ERROR_SIZE] = { retcode = FILE *fp = fopen(, curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); curl_easy_setopt(curl, CURLOPT_HTTPGET, ); curl_easy_setopt(curl, CURLOPT_USERAGENT, () curl_easy_setopt(curl, CURLOPT_WRITEDATA, ( *)fp); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetContent); curl_easy_setopt(curl, CURLOPT_WRITEDATA, ( *)&RevBuff); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FuncCallBack); ret = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode); size_t GetContent( *buffer, size_t size, size_t nmemb, * FILE *fp = (FILE * size_t return_size = size_t FuncCallBack( *data, size_t size, size_t nmemb, * size_t sizes = size * (NULL == writerData-> }