繁体   English   中英

在C ++中,使用队列和libcurl时出现错误

[英]In C++ I'm getting an error when using a queue and libcurl

我正在使用C ++和libcurl制作Web Spider。 我遇到一个问题,当我使用队列获取URL时,我收到一条错误消息,说不支持HTTP。 错误是: http://google.com* Protocol "http not supported or disabled in libcurl* Unsupported protocol 。这是我的代码:

#include <iostream>
#include <queue>
#include <curl/curl.h>

std::string data; //will hold the url's contents

size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{ //callback must have this declaration
    //buf is a pointer to the data that curl has for us
    //size*nmemb is the size of the buffer

    for (int c = 0; c<size*nmemb; c++)
    {
        data.push_back(buf[c]);
    }
    return size*nmemb; //tell curl how many bytes we handled
}


int main(int argc, const char * argv[])
{
    std::queue<std::string> Q;
    Q.push("http://google.com");

    while (!Q.empty()) {
        std::string url = Q.front();
        std::cout << Q.front();
        CURL* curl; //our curl object

        curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
        curl = curl_easy_init();

        curl_easy_setopt(curl, CURLOPT_URL, &url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress

        curl_easy_perform(curl);

        std::cout << std::endl << data << std::endl;
        std::cin.get();

        curl_easy_cleanup(curl);
        curl_global_cleanup();
        data.erase();
        Q.pop();
    }

    return 0;
}

我是C ++的新手。 我应该向哪个方向寻找解决方法?

这是错误的:

curl_easy_setopt(curl, CURLOPT_URL, &url);

这是对的:

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

std::string不是char* 你不能说

curl_easy_setopt(curl, CURLOPT_URL, &url);

因为这将std::string*作为选项而不是char* 正确的方法是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM