Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。
http://curl.haxx.se/libcurl/
从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.PM...等参数,没有具体意义,就是long,bool,int,char*等进行特殊封装过,你可以用普通类型取代。
2.关键在curl_easy_setopt函数设置option,可以设置ftp,http,get,post等许多选项,请根据具体使用情况设置。
3.对取回来的数据需要进行判断,比如http下载文件,如果文件不存在,需要进行处理。因为writer是可以将buf填充404 not found等网页内容的,不能将这个内容当成文件内容,所以需要判断http web返回来的code,进行判断。
4.我有个问题,就是想得到服务器上filename的具体名称,verbose调试已经返回了,但是我在getinfo的时候,试过好多选项,但未找到这个存放真实服务器文件名的选项,如果有知道的麻烦告诉我,谢谢了!
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib")
PM_ULONG writer(PM_VOID *data, PM_INT size, PM_INT nmemb, PmString &content);
PM_BOOL CurlInit(CURL *&curl, PM_LPCSTR url,PmString &content);
PM_BOOL GetURLDataBycurl(PM_LPCSTR URL, PmString &content);
void main()
{
//PM_LPSTR url="http://down1.chinaunix.net/distfiles/libpcap-1.0.20050129.tar.gz";
PM_LPCSTR url ="http://www.baidu.com/img/baidu.gif";
PmString content;
if ( GetURLDataBycurl(url,content))
{
printf("%s\n",content);
}
getchar();
}
PM_BOOL CurlInit(CURL *&curl, PM_LPCSTR url,PmString &content)
{
CURLcode code;
PmString error;
curl = curl_easy_init();
if (curl == NULL)
{
debug0( "Failed to create CURL connection\n");
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
if (code != CURLE_OK)
{
debug1( "Failed to set error buffer [%d]\n", code );
return PM_FALSE;
}
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
code = curl_easy_setopt(curl, CURLOPT_URL, url);
if (code != CURLE_OK)
{
debug1("Failed to set URL [%s]\n", error);
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
if (code != CURLE_OK)
{
debug1( "Failed to set redirect option [%s]\n", error );
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK)
{
debug1( "Failed to set writer [%s]\n", error);
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
if (code != CURLE_OK)
{
debug1( "Failed to set write data [%s]\n", error );
return PM_FALSE;
}
return PM_TRUE;
}
PM_ULONG writer(PM_VOID *data, PM_INT size, PM_INT nmemb, PmString &content)
{
PM_ULONG sizes = size * nmemb;
PmString temp(data,sizes);
content += temp;
return sizes;
}
PM_BOOL GetURLDataBycurl(PM_LPCSTR URL, PmString &content)
{
CURL *curl = NULL;
CURLcode code;
PmString error;
code = curl_global_init(CURL_GLOBAL_DEFAULT);
if (code != CURLE_OK)
{
debug1( "Failed to global init default [%d]\n", code );
return PM_FALSE;
}
if ( !CurlInit(curl,URL,content) )
{
debug0( "Failed to global init default [%d]\n" );
return PM_FALSE;
}
code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
debug2( "Failed to get '%s' [%s]\n", URL, error);
return PM_FALSE;
}
PM_ULONG retcode = 0;
code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (code == CURLE_OK) && retcode == 200 )
{
PM_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 PM_TRUE;
}
else
{
// debug1( "%s \n ",getStatusCode(retcode));
return PM_FALSE;
}
curl_easy_cleanup(curl);
return PM_FALSE;
}