curl WRITEFUNCTION and classes

后端 未结 3 808
名媛妹妹
名媛妹妹 2020-12-06 11:33
class Filter{
private:
    string contents;
    bool Server(void);
public:
    void handle(void *, size_t, size_t, void *);
   };

i have a class he

相关标签:
3条回答
  • 2020-12-06 12:18
    string temp;
    
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,handle);
    curl_easy_setopt(curl,CURLOPT_WRITEDATA,&temp);
    
    size_t Filter::handle(void *ptr, size_t size, size_t nmemb, string stream)
    {
        string temp(static_cast<const char*>(ptr), size * nmemb);
        stream = temp;
        return size*nmemb;
    }
    

    thats how i got it to work.. this will save the website to the string named temp.

    0 讨论(0)
  • 2020-12-06 12:22

    handle must be a static member function. You can pass a pointer to the instance of Filter as last argument by using CURLOPT_WRITEDATA

    class Filter
    { 
    private:
        std::string content_;
        static size_t handle(char * data, size_t size, size_t nmemb, void * p);
        size_t handle_impl(char * data, size_t size, size_t nmemb);
    };
    
    size_t Filter::handle(char * data, size_t size, size_t nmemb, void * p)
    {
        return static_cast<Filter*>(p)->handle_impl(data, size, nmemb);
    }
    
    size_t Filter::handle_impl(char* data, size_t size, size_t nmemb)
    {
        content_.append(data, size * nmemb);
        return size * nmemb;
    }
    
    int main()
    {
       // curl initialization... should be done in a constructor
       Filter f;
       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Filter::handle);
       // curl cleanup... should be done in a destructor
       return 0;
    }
    
    0 讨论(0)
  • 2020-12-06 12:25

    I am using curlpp:

    std::stringstream result;
    
    request.setOpt(cURLpp::Options::WriteStream(&result));
    request.perform();
    

    this will save the reply webserver to the stringstream named result.

    0 讨论(0)
提交回复
热议问题