日志文章

2007年06月12日 09:09:16

在Windows下使用libcurl、curlpp

1. 下载libcurl、curlpp源代码
例如下载
 
2. 下载zlib
例如下载
 
3. 编译OpenSSL
 
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.测试

该程序是备份CUBlog程序的C++版,需要boost库

 

#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;
}

Tags: curl   curlpp   windows  

类别: curl |  评论(8) |  浏览(1403) |  收藏
8楼 [匿名]7ym7goj4 2009年06月18日 03:20:30 Says:
12.30b%3Ca%20href="http://www.3tt8.com/"%3E%E5%AE%AB%E9%A2%88%E7%B3%9C%E7%83%82%3C/a%3E%0D%0A12.26y%3Ca%20href="http://www.forevernikes.com/cheapshirts.html"%3ECHEAP%20SHIRTS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/cheaphandbags.html"%3ECHEAP%20HANDBAGS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/replicahandbags.html"%3EREPLICA%20HANDBAGS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/coachbags.html"%3ECOACH%20BAGS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/wholesaletshirts.html"%3EWHOLESALE%20T%20SHIRTS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/jordanshoes.html"%3EJORDAN%20SHOES%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/cheapjordans.html"%3ECHEAP%20JORDANS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/wholesalejordans.html"%3EWHOLESALE%20JORDANS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/replicawatches.html"%3EREPLICA%20WATCHES%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/uggboots.html"%3EUGG%20BOOTS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/timberlandboots.html"%3ETIMBERLAND%20BOOTS%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/pradashoes.html"%3EPRADA%20SHOES%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/guccishoes.html"%3EGUCCI%20SHOES%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/"%3EAIR%20MAX%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/"%3ENIKE%20SHOX%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/"%3ENIKE%20SHOES%3C/a%3E%0D%0A%3Ca%20href="http://www.forevernikes.com/"%3EAIR%20JORDANS%3C/a%3E%0D%0Ab%3Ca%20href="http://www.csjbaojie.com/"%3E%E4%B8%8A%E6%B5%B7%E4%BF%9D%E6%B4%81%E5%85%AC%E5%8F%B8%3C/a%3E%20%0D%0A%3Ca%20href="http://www.csjbaojie.com/"%3E%E4%BF%9D%E6%B4%81%E5%85%AC%E5%8F%B8%3C/a%3E%20%0D%0A12.25B%3Ca%20href="http://www.ecoswaycc.com/"%3E%E7%A7%91%E5%A3%AB%E5%A8%81%3C/a%3E%0D%0A%3Ca%20href="http://www.ecoswaycc.com/"%3Eecosway%3C/a%3E%0D%0A12.22%3Ca%20href="http://www.bjdoudou.com/"%3E%E9%9D%92%E6%98%A5%E7%97%98%3C/a%3E%0D%0A%3Ca%20href="http://www.bjdoudou.com/"%3E%E7%B2%89%E5%88%BA%3C/a%3E%0D%0A%3Ca%20href="http://www.bjdoudou.com/dd.html"%3E%E7%97%98%E7%97%98%3C/a%3E%0D%0A%3Ca%20href="http://www.bjdoudou.com/zlcc.html"%3E%E6%B2%BB%E7%96%97%E7%97%A4%E7%96%AE%3C/a%3E%0D%0A%3Ca%20href="http://www.bjdoudou.com/zlcc.html"%3E%E7%97%A4%E7%96%AE%3C/a%3E%0D%0A12.20bq3%3Ca%20href="http://www.rxcmyk.com/"%3E%E6%8B%BC%E5%9B%BE%3C/a%3E%0D%0A12.19%3Ca%20href="http://www.wybb666.com/wybbdzl.html"%3E%E5%A4%96%E9%98%B4%E7%99%BD%E6%96%91%E7%9A%84%E6%B2%BB%E7%96%97%3C/a%3E%0D%0A%3Ca%20href="http://www.wybb666.com/wybbysmyh.html"%3E%E5%A4%96%E9%98%B4%E7%99%BD%E6%96%91%E7%94%A8%E4%BB
7楼 [匿名]d8lrdwrm 2009年06月17日 15:44:38 Says:
%E7%BD%91%E7%AB%99%E6%8E%A8%E5%B9%BF%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%8F%91%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%BB%BA%E8%BD%AF%E4%BB%B6,%E7%BD%91%E7%BB%9C%E8%90%A5%E9%94
6楼 [匿名]pq2lrt8y 2009年06月10日 14:59:51 Says:
%E7%BD%91%E7%AB%99%E6%8E%A8%E5%B9%BF%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%8F%91%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%BB%BA%E8%BD%AF%E4%BB%B6,%E7%BD%91%E7%BB%9C%E8%90%A5%E9%94
5楼 [匿名]enib7y0y 2009年06月05日 13:24:40 Says:
%E7%BD%91%E7%AB%99%E6%8E%A8%E5%B9%BF%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%8F%91%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%BB%BA%E8%BD%AF%E4%BB%B6,%E7%BD%91%E7%BB%9C%E8%90%A5%E9%94
4楼 [匿名]test 2008年11月01日 00:18:21 Says:
测试了一下,竟然能编译通过,真是不明白其中奥秘。希望博主能解释一下,谢谢。
QQ524103841
3楼 [匿名]test 2008年10月31日 23:52:54 Says:
我虽然没有尝试做,但是看了博主文章中静态成员函数访问了类非静态成员,这个是如何做到的呢?可以解释一下嘛?
我自己写的一个类,由于回调函数必须为静态成员函数,但是不能访问类成员变量,是个很大问题。所以请教一下博主。
QQ524103841
2楼 [匿名]atempcode 2008年09月01日 21:17:36 Says:
@1楼 [匿名]飞翔的西瓜


#ifndef __DLLFCT__H_
#define __DLLFCT__H_

/*
* Decorate exportable functions for Win32 DLL linking.
* This avoids using a .def file for building libcurl.dll.
*/
#if (defined(WIN32) || defined(_WIN32)) && !defined(CURLPP_STATICLIB)
//#if defined(BUILDING_CURLPP)
//#define CURLPPAPI __declspec(dllexport)
//#else
//#define CURLPPAPI __declspec(dllimport)
//#endif
//#else
#define CURLPPAPI
#endif

#endif
1楼 [匿名]飞翔的西瓜 2008年03月25日 15:42:12 Says:
你好,这篇文章对我来说真的是雪中送炭,感谢博主的分享精神!
我依照着步骤,进行到了以下位置:
1,修改Makefile.msvc中LIBCURL_PATH
2,修改dllfct.h关于CURLPPAPI的宏定义#define CURLPPAPI
第一步我已经修改,第二步不太清楚具体如何修改?还请博主赐教!
如果能发邮件到kevenqu@gmail.com,将感激不尽!提前说声谢谢!

屈威

发表评论
看不清楚,换一张