Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 在Windows下使用libcurl、curlpp
【标  题】:在Windows下使用libcurl、curlpp
【关键字】:Windows,libcurl,curlpp
【来  源】:http://www.cublog.cn/u/8780/showart.php?id=278339

在Windows下使用libcurl、curlpp

Your Ad Here
1. 下载libcurl、curlpp源代码
http://curl.haxx.se/
http://rrette.com/textpattern/index.php?s=cURLpp
例如下载
http://curl.haxx.se/download/curl-7.16.2.tar.bz2
http://rrette.com/files/curlpp/curlpp-0.7/curlpp-0.7.0.tar.gz
 
2. 下载zlib
http://www.zlib.net/
例如下载
http://www.zlib.net/zlib123-dll.zip
 
3. 编译OpenSSL
参考Windows下Openssl安装以及编程(Visual C++版)
 
4. 编译libcurl,以VS2005为例

将curl-7.16.2.tar.bz2解压到某目录,例如C:\curl,进入C:\curl\lib目录

将zlib123-dll.zip解压到某目录,例如C:\zlib

设Openssl的目录为C:\openssl

 

进入Visual Studio 2005命令提示,进入C:\curl\lib

 

编译Debug版本。

set CFG=debug-dll-ssl-dll-zlib-dll

set OPENSSL_PATH=C:/openssl

set ZLIB_PATH=C:/zlib/include

nmake -f Makefile.vc8

 

其输出:libcurld_imp.lib, libcurld.dll

 

编译Release版本。

set CFG=release-dll-ssl-dll-zlib-dll

set OPENSSL_PATH=C:/openssl

set ZLIB_PATH=C:/zlib/include

nmake -f Makefile.vc8

其输出:libcurl_imp.lib, libcurl.dll

如果需要编译其他版本,可查看设定相应的CFG 参数即可。

 

 

5.编译curlpp

将curlpp-0.7.0.tar.gz解压到某目录,例如C:\curlpp\curlpp

set CFG=release

nmake -f Makefile.msvc

如果需要编译其他版本,可查看设定相应的CFG 参数即可。

 

需要注意的是可能需要对原文件进行一定的修改。

一种方案是:

修改Makefile.msvc中LIBCURL_PATH

修改dllfct.h关于CURLPPAPI的宏定义

#define CURLPPAPI 

 

6.测试

在VS2005中新建一WIN32控制台工程。设置好Include Path和Lib Path

 

#include <iostream>
#include <string>
#include <queue>

#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>

#pragma comment(lib,"libcurlpp.lib")
#pragma comment(lib,"libcurl_imp.lib")

using namespace std;

#define MAX_FILE_LENGTH 20000

class WriterMemoryClass
{
    public:
    // Helper Class for reading result from remote host
    WriterMemoryClass(){
        this->m_pBuffer = NULL;
        this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
        this->m_Size = 0;
    }
    ~WriterMemoryClass() {
        if (this->m_pBuffer)
            free(this->m_pBuffer);
    }
    void* Realloc(void* ptr, size_t size) {
        if(ptr)
            return realloc(ptr, size);
        else
            return malloc(size);
    }
    // Callback must be declared static, otherwise it won't link...
    size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb) {
        // Calculate the real size of the incoming buffer
        size_t realsize = size * nmemb;
        // (Re)Allocate memory for the buffer
        m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize);
        // Test if Buffer is initialized correctly & copy memory
        if (m_pBuffer == NULL) {
            realsize = 0;
        }
        
        memcpy(&(m_pBuffer[m_Size]), ptr, realsize);
        m_Size += realsize;
        // return the real size of the buffer...
        return realsize;
    }
    
    // Public member vars
    char* m_pBuffer;
    size_t m_Size;
};

int main(int argc,char * argv[])
{
    string list_base="http://blog.chinaunix.net/u/8780/article.php?frmid=0&page=";
    string art_base="http://blog.chinaunix.net/u/8780/showart.php?id=";
    boost::regex rexp("<a href=\"showart_([0-9]{6}).*?><font.*?><b>(.*?)</b></font></a>");
    boost::regex fnp("[/\\:*?\"<>]");
    typedef boost::tuple<string,string> element;
    queue<element> q;
    bool save=true;
    if(argc==1)
        save=false;
        
    try {
        cURLpp::Cleanup cleaner;
        cURLpp::Easy request;
        cURLpp::Easy handler;
       int i=0;
        bool cond=true;
        while(cond) {
            i++;
            cond=false;
            string list_url=list_base+boost::lexical_cast<string>(i);
            cerr<<list_url<<endl;
            // Get the content
            WriterMemoryClass mWriterChunk;
            // Set the writer callback to enable cURL to write result in a memory area
            cURLpp::Types::WriteFunctionFunctor functor(&mWriterChunk,&WriterMemoryClass::WriteMemoryCallback);
            cURLpp::Options::WriteFunction *test = new cURLpp::Options::WriteFunction(functor);
            request.setOpt(test);
            // Setting the URL to retrive.
            request.setOpt(new cURLpp::Options::Url(list_url));
            //request.setOpt(new cURLpp::Options::Verbose(true));
            request.perform();
            //cout<<mWriterChunk.m_pBuffer<<endl;
            // Get the id and article name pairs
            string s=string(mWriterChunk.m_pBuffer);
            string::const_iterator it=s.begin();
            string::const_iterator end=s.end();
            boost::smatch m;
            while(boost::regex_search(it,end,m,rexp)) {
                cond=true;
                //cout<<m[1].str()<<" "<<m[2].str()<<endl;
                q.push(element(m[1].str(),m[2].str()));
                it=m[0].second;
            }
        }
        // Backup
        cerr<<"There are "<<i-1<<" Pages"<<endl;
        cout<<"<html>\n<head>"<<endl;
        cout<<"<title>Linxh's CUBlog List</title>"<<endl;
        cout<<"</head>\n<body>"<<endl;
        cout<<"<font face=\"Arial, Helvetica\" size=4>"<<endl;
        
        while(!q.empty()) {
            element e=q.front();
            q.pop();
            string art_url=art_base+e.get<0>();
            string fname=boost::tuples::get<1>(e);
            cout<<"<a target=\"_blank\" href=\""<<art_url<<"\">"<<fname<<"</a><br>"<<endl;
            if(save) {
                fname=boost::regex_replace(fname,fnp,"_");
                fname+=".html";
                cerr<<"Saving "<<art_url<<" as file "<<fname.c_str()<<endl;
                FILE * fp=fopen(fname.c_str(),"wb");
                if(fp==NULL) {
                    cerr<<"Could not open file "<<fname.c_str()<<endl;
                    cerr<<"Skipping"<<endl;
                    continue;
                }
                cURLpp::OptionTrait< void *, CURLOPT_WRITEDATA > myData(fp);
                handler.setOpt(myData);
                // Setting the URL to retrive.


                handler.setOpt(new cURLpp::Options::Url(art_url));
                //request.setOpt(new cURLpp::Options::Verbose(true));
                handler.perform();
            }
        }
        cout<<"</font>"<<endl;
        cout<<"</body>\n</html>"<<endl;
        
    }
    catch ( cURLpp::LogicError & e ) {
        std::cout << e.what() << std::endl;
    }
    catch ( cURLpp::RuntimeError & e ) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

mysql备份脚本:【上一篇】
在VS2005中使用libcurl:【下一篇】
【相关文章】
  • 《给初学者的Windows Vista的补遗手册》之072
  • C#中如何建立Windows服務
  • 浅谈Windows CE应用程序的可移植性
  • Windows ListCtrl使用技巧
  • Vista中Windows Service需要桌面交互时
  • The USB Webcam driver in Windows CE
  • Windows Vista 开发新特性(1)- 有关重启管理器 Restart Manager
  • 《给初学者的Windows Vista的补遗手册》之068
  • 《给初学者的Windows Vista的补遗手册》之069
  • 《给初学者的Windows Vista的补遗手册》之070
  • 【随机文章】
  • AIX 系统命令简介
  • 最后一关攻克(应该是最后一关了吧)
  • 巧妙解救被“绑架”的浏览器
  • DirectDraw之C#入门攻略
  • Python指南-2-使用Python解释器
  • 葬花吟
  • 设备软件优化.DSO 简介
  • Optional Primary Network Name的设置
  • 网上保障隐私十大秘技
  • python(15): if语句及逻辑判断(第9章)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.