`
dewei
  • 浏览: 163086 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

VC++调用libcurl的VC库使用详解

阅读更多

1、下载官方库。

地址:http://curl.haxx.se/download.html#Win32  下载  Win32 - MSVC,下面有两个版本的库,一个是带ssl的,一个是不带ssl的。

不带ssl的:http://curl.haxx.se/download/libcurl-7.18.0-win32-msvc.zip

带ssl的:http://curl.haxx.se/download/libcurl-7.19.3-win32-ssl-msvc.zip

 

2、在VS2010中VC++目录中加入从zip中解压的include目录,和lib目录

 

3、使用方法为,注意还有debug和Release两种库

#include <curl/curl.h>
//不带SSL
#pragma comment(lib, "libcurl.lib")
//带SSL
#pragma comment(lib, "libcurl_imp.lib")
 

4、 下面是我写的用于http和https的学习记录

// libcurl http和https学习记录 2012-6-30 by Dewei
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <assert.h> 
#include "curl/curl.h"
#pragma comment(lib, "libcurl_imp.lib")

#define  SKIP_PEER_VERIFICATION 1
//#define  SKIP_HOSTNAME_VERFICATION 1

/*
ptr是指向存储数据的指针,
size是每个块的大小,
nmemb是指块的数目,
stream是用户参数。
所以根据以上这些参数的信息可以知道,ptr中的数据的总长度是size*nmemb
*/
size_t call_wirte_func(const char *ptr, size_t size, size_t nmemb, std::string *stream)
{
    assert(stream != NULL);
    size_t len  = size * nmemb;
    stream->append(ptr, len);
    return len;
}
// 返回http header回调函数  
size_t header_callback(const char  *ptr, size_t size, size_t nmemb, std::string *stream)  
{  
    assert(stream != NULL);
    size_t len  = size * nmemb;
    stream->append(ptr, len);
    return len;
}  

int _tmain(int argc, _TCHAR* argv[])
{


    CURL *curl;
    CURLcode code;
    std::string szbuffer;
    std::string szheader_buffer;
    char errorBuffer[CURL_ERROR_SIZE];
    std::string url = "http://www.douban.com";
    //std::string url = "https://vip.icbc.com.cn/icbc/perbank/index.jsp";
    std::string useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1";
    /*
    CURL_GLOBAL_ALL                //初始化所有的可能的调用。
    CURL_GLOBAL_SSL               //初始化支持 安全套接字层。
    CURL_GLOBAL_WIN32            //初始化win32套接字库。
    CURL_GLOBAL_NOTHING         //没有额外的初始化。
    */
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if(curl) {
        // 远程URL,支持 http, https, ftp
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent.c_str());
        // 官方下载的DLL并不支持GZIP,Accept-Encoding:deflate, gzip
        curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate");          
        //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//调试信息打开
        //https 访问专用:start
#ifdef SKIP_PEER_VERIFICATION
        //跳过服务器SSL验证,不使用CA证书
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        //如果不跳过SSL验证,则可指定一个CA证书目录
        //curl_easy_setopt(curl, CURLOPT_CAPATH, "this is ca ceat");
#endif

#ifdef SKIP_HOSTNAME_VERFICATION
        //验证服务器端发送的证书,默认是 2(高),1(中),0(禁用)
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
        //https 访问专用:end


        //发送cookie值给服务器
        //curl_easy_setopt(curl, CURLOPT_COOKIE, "name1=var1; name2=var2;"); 
        /* 与服务器通信交互cookie,默认在内存中,可以是不存在磁盘中的文件或留空 */
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "./cookie.txt"); 
        /* 与多个CURL或浏览器交互cookie,会在释放内存后写入磁盘文件 */
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "./cookie.txt"); 

        /* POST 数据 */
        // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
        //设置重定向的最大次数
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
        //设置301、302跳转跟随location
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        //抓取内容后,回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, call_wirte_func);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &szbuffer);
        //抓取头信息,回调函数
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback );
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, &szheader_buffer);

        /* 
        CURLE_OK	任务完成一切都好
        CURLE_UNSUPPORTED_PROTOCOL	不支持的协议,由URL的头部指定
        CURLE_COULDNT_CONNECT	不能连接到remote 主机或者代理
        CURLE_REMOTE_ACCESS_DENIED	访问被拒绝
        CURLE_HTTP_RETURNED_ERROR	Http返回错误
        CURLE_READ_ERROR	读本地文件错误 
        CURLE_SSL_CACERT	访问HTTPS时需要CA证书路径
        */
        code = curl_easy_perform(curl);
        if(CURLE_OK == code) {
            double val;

            /* check for bytes downloaded */
            code = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &val);
            if((CURLE_OK == code) && (val>0))
                printf("Data downloaded: %0.0f bytes.\n", val);

            /* check for total download time */
            code = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &val);
            if((CURLE_OK == code) && (val>0))
                printf("Total download time: %0.3f sec.\n", val);

            /* check for average download speed */
            code = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &val);
            if((CURLE_OK == code) && (val>0))
                printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);

            printf("%s\n",szbuffer.c_str()); 
        } 
        else {
            fprintf(stderr, "Failed to get '%s' [%s]\n", url, errorBuffer);
            // exit(EXIT_FAILURE);
        }

        /* 释放内存 */
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    getchar();
    return 0;
}
 

 

提示:

若没有找到zlib1.dll文件。

http://gnuwin32.sourceforge.net/downlinks/zlib-bin-zip.php 下载zib包,将dll文件拷贝至debug目录,程序即可正常执行了。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics