浪迹天涯

唯有努力...
努力....再努力...

libcurl使用心得

Libcurl为一个免费开源的,客户端url传输库,支持FTPFTPSTFTPHTTPHTTPSGOPHERTELNETDICTFILELDAP,跨平台,支持WindowsUnixLinux等,线程安全,支持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;
}

posted on 2008-06-28 14:50 浪迹天涯 阅读(2079) 评论(3)  编辑 收藏 引用 所属分类: Lib

评论

# re: libcurl使用心得 2008-06-28 15:42 LOGOS

看起来能用来做蜘蛛  回复  更多评论   

# re: libcurl使用心得 2008-06-28 16:00 浪迹天涯

有用这个写爬虫的  回复  更多评论   

# re: libcurl使用心得 2008-07-01 12:48 企业即时通讯

太厉害了。哈哈。  回复  更多评论   


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:



<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

公告

宠辱不惊 看庭前花开花落;
去留无意 望天上云卷云舒.
MSN:qj-qiujian@sohu.com
近期研究
libcurl gsoap libpcap

Dict.CN 在线词典, 英语学习, 在线翻译

常用链接

留言簿(1)

随笔分类(29)

随笔档案(29)

文章分类(10)

文章档案(10)

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜