0

I would like to get source code of a page, I have this function to populate a string.

My header file :

class Explorateur {
public:
    Explorateur();
    ~Explorateur();
    std::string RecupererCodeSource(std::string pAdresse);

    static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
    {
        ((std::string*)userp)->append((char*)contents, size * nmemb);
        return size * nmemb;
    }

};

My class file :

std::string Explorateur::RecupererCodeSource(std::string pAdresse)
{
      CURL *curl;
      CURLcode res;
      std::string readBuffer;

      curl = curl_easy_init();
      if(curl)
      {
        curl_easy_setopt(curl, CURLOPT_URL, pAdresse);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        std::cout << "curl  : " << curl << std::endl;
        res = curl_easy_perform(curl);
        std::cout << "res  : " << res << std::endl;
        curl_easy_cleanup(curl);

      }
      std::cout << "Read : " << readBuffer << std::endl;
      return readBuffer;
}

My main :

int main()
{
    Explorateur explorateur;
    std::string valeur = explorateur.RecupererCodeSource("https://www.google.com/");
    return 0;
}

The result is not excepted, it's empty, do you have an idea why ?

curl  : 0x1450d60
res  : 6
Read :
4
  • 1
    The curl_easy_perform returned CURLE_COULDNT_RESOLVE_HOST (6) curl.haxx.se/libcurl/c/libcurl-errors.html. What content do you expect to see? Commented May 17, 2017 at 20:58
  • I except the Google's page source code... Commented May 17, 2017 at 21:04
  • 1
    Can you change curl_easy_setopt(curl, CURLOPT_URL, pAdresse); to ` curl_easy_setopt(curl, CURLOPT_URL, pAdresse.c_str());`? Commented May 17, 2017 at 21:11
  • @Anonymous: why do you expect to see any content when curl_easy_perform() reports failure? It is clear that it is having trouble finding Google to begin with, so it can't download anything. And the reason is because you are not passing the correct memory address for the URL string to curl, so it fails to find the server. Commented May 17, 2017 at 23:39

1 Answer 1

1

Lib Curl being a C library follows an old "good" variadic argument design. That is why you code is doing completely unexpected thing while compiling fine.

Having

CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...) 

and this in your code

curl_easy_setopt(curl, CURLOPT_URL, pAdresse);

produces the behaviour you are witnessing.

Change in you code to this

curl_easy_setopt(curl, CURLOPT_URL, pAdresse.c_str());

or even better if you use C++ >= 11 to this

curl_easy_setopt(curl, CURLOPT_URL, pAdresse.data());

And it is alive)).

Sign up to request clarification or add additional context in comments.

4 Comments

In C++11 and later, c_str() and data() return the same thing (a pointer to a null-terminated C string), so there is no reason to switch from c_str() to data(), especially when c_str() in every C++ version returns exactly what curl is expecting.
IMHO c_str looks more ugly + data brings more consistency to the code with other std containers and c++17 non-const data )).
c_str better conveys that a C-style string is being used. And you wouldn't need to use a non-const data in this situation anyway.
I did not mean this particular snippet, I meant overall consistency of the code using std containers in any cases. c_str is not the best naming for what it is meant, more this names more of a flavour of some c-compatible function for some legacy/wrapping API, than a member function of a standard library of a very different programming language. In this spirit data is quite good alternative, even might be some other better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.