Login dark

author: love02xp
title: libcurl使用心得-免费开源url传输库
date: 2017-03-25 10:00:45
category: [编程代码]
tags: [学习,编程]


[阅读原文](http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html)

Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。

<!--more-->


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.关键在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;
}