curl problems in c++ class
Posted
by
Danilo
on Stack Overflow
See other posts from Stack Overflow
or by Danilo
Published on 2012-12-01T17:34:53Z
Indexed on
2012/12/02
11:05 UTC
Read the original article
Hit count: 131
I read a few articles on c++ / curl here on stackoverflow and assembled the following. The main goal is to handle the whole request in an instance of a class -- and maybe later in a secondary thread.
My problem is: "content_" seems to stay empty though its the same addr and
HttpFetch.h:
class HttpFetch
{
private:
CURL *curl;
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);
public:
std::string content_;
static std::string url_;
HttpFetch(std::string url);
void start();
std::string data();
};
HttpFetch.cpp:
HttpFetch::HttpFetch(std::string url) {
curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
curl = curl_easy_init();
content_.append("Test");
std::cout << &content_ << "\n";
curl_easy_setopt(curl, CURLOPT_URL, &url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content_);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &HttpFetch::handle);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
//std::cout << &content_ << "\n";
}
void HttpFetch::start() {
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
size_t HttpFetch::handle(char * data, size_t size, size_t nmemb, void * p)
{
std::string *stuff = reinterpret_cast<std::string*>(p);
stuff->append(data, size * nmemb);
std::cout << stuff << "\n"; // has content from data in it!
return size * nmemb;
}
main.cpp:
#include "HttpFetch.h"
int main(int argc, const char * argv[])
{
HttpFetch call = *new HttpFetch("http://www.example.com");
call.start();
::std::cout << call.content_ << "\n"
}
Thanks in advance
© Stack Overflow or respective owner