Save cURL content result into a string in C++

前端 未结 7 1888
鱼传尺愫
鱼传尺愫 2020-11-30 22:16
int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, \"http://www.google.com\");
    curl_         


        
7条回答
  •  旧时难觅i
    2020-11-30 22:53

    On my blog I have published a simple wrapper class to perform this task.

    Usage example:

    #include "HTTPDownloader.hpp"
    
    int main(int argc, char** argv) {
        HTTPDownloader downloader;
        std::string content = downloader.download("https://stackoverflow.com");
        std::cout << content << std::endl;
    }
    

    Here's the header file:

    /**
     * HTTPDownloader.hpp
     *
     * A simple C++ wrapper for the libcurl easy API.
     *
     * Written by Uli Köhler (techoverflow.net)
     * Published under CC0 1.0 Universal (public domain)
     */
    #ifndef HTTPDOWNLOADER_HPP
    #define HTTPDOWNLOADER_HPP
    
    #include 
    
    /**
     * A non-threadsafe simple libcURL-easy based HTTP downloader
     */
    class HTTPDownloader {
    public:
        HTTPDownloader();
        ~HTTPDownloader();
        /**
         * Download a file using HTTP GET and store in in a std::string
         * @param url The URL to download
         * @return The download result
         */
        std::string download(const std::string& url);
    private:
        void* curl;
    };
    
    #endif  /* HTTPDOWNLOADER_HPP */
    

    Here's the source code:

    /**
     * HTTPDownloader.cpp
     *
     * A simple C++ wrapper for the libcurl easy API.
     *
     * Written by Uli Köhler (techoverflow.net)
     * Published under CC0 1.0 Universal (public domain)
     */
    #include "HTTPDownloader.hpp"
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
        string data((const char*) ptr, (size_t) size * nmemb);
        *((stringstream*) stream) << data;
        return size * nmemb;
    }
    
    HTTPDownloader::HTTPDownloader() {
        curl = curl_easy_init();
    }
    
    HTTPDownloader::~HTTPDownloader() {
        curl_easy_cleanup(curl);
    }
    
    string HTTPDownloader::download(const std::string& url) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); //Prevent "longjmp causes uninitialized stack frame" bug
        curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate");
        std::stringstream out;
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
        /* Perform the request, res will get the return code */
        CURLcode res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
        }
        return out.str();
    }
    

提交回复
热议问题