LoginSignup
2
2

More than 5 years have passed since last update.

備忘:cURLppを使ってみた

Posted at

C++のHTTPクライアントライブラリのcURLpp(cURLのC++ラッパ)を使ってみたので、
curlpp を使ってみる - GET 編 - 私の日常を参考(そのまま)にサンプルコードを作ってみた。

curlpp_exam.cpp
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <iostream>

using namespace std;
using namespace cURLpp::Options;

class CurlExam {
public:
        string m_content;

        bool get(const string &url)
        {
                try {
                        cURLpp::Cleanup cleaner;
                        cURLpp::Easy    req;
                        req.setOpt(new Url(url));
                        req.setOpt(new WriteFunction(
                                cURLpp::Types::WriteFunctionFunctor(this, &CurlExam::writeMemoryCallback))
                        );

                        req.perform();
                } catch (cURLpp::LogicError & e) {
                        return false;
                } catch (cURLpp::RuntimeError & e) {
                        return false;
                }
                return true;
        }

private:
        size_t writeMemoryCallback(char *pstr, size_t size, size_t nmemb)
        {
                size_t allsize = size * nmemb;

                m_content.append(static_cast<const char *>(pstr), allsize);

                return allsize;
        }
};

int main(void)
{
        CurlExam cx;
        bool ret;

        ret = cx.get("http://blog.alaif.net/");
        if (ret) {
                cout << cx.m_content;
        }

        return ret;
}

ビルドは、


clang++ -I/usr/local/include -L/usr/local/lib -lcurl -lcurlpp curlpp_sample.cpp -o curlpp_sample

で。

うん、簡単にできるもんだ。

次は、C++11の機能も使って書いてみよう。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2