zoukankan      html  css  js  c++  java
  • libcurl使用心得-包括下载文件不存在处理相关(转)

    libcurl使用心得

    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.关键在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
    ",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
    ");
            return false;
        }
        code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
        if (code != CURLE_OK)
        {
            printf( "Failed to set error buffer [%d]
    ", 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]
    ", error);
            return false;
        }
        code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        if (code != CURLE_OK)
        {
            printf( "Failed to set redirect option [%s]
    ", error );
            return false;
        }
        code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        if (code != CURLE_OK)
        {
            printf( "Failed to set writer [%s]
    ", error);
            return false;
        }
        code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
        if (code != CURLE_OK)
        {
            printf( "Failed to set write data [%s]
    ", 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]
    ", code );
            return false;
        } 
        
        if ( !CurlInit(curl,URL,content) )
        {
            printf( "Failed to global init default [%d]
    " );
            return PM_FALSE;
        }
        code = curl_easy_perform(curl);
        if (code != CURLE_OK)
        {
            printf( "Failed to get '%s' [%s]
    ", 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 
     ",getStatusCode(retcode));
            return false;
        }
        curl_easy_cleanup(curl);
        return false;
    }

    原地址:http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html

  • 相关阅读:
    ZoomBar 设计
    旋转toast 自定义toast方向,支持多个方向的显示,自定义View
    NA
    ISCSI共享
    DFS序
    矩阵快速幂
    SOJ4389 川大贴吧水王 队列
    ST表学习总结
    HDU 5724 Chess(SG函数)
    2017 计蒜之道 初赛 第一场 A、B题
  • 原文地址:https://www.cnblogs.com/wainiwann/p/3502326.html
Copyright © 2011-2022 走看看