我正在使用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 ++的新手。我应该从哪个方向了解如何解决这个问题?
答案 0 :(得分:3)
这是错误的:
curl_easy_setopt(curl, CURLOPT_URL, &url);
这是对的:
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
答案 1 :(得分:3)
std::string
不是char*
。你不能说
curl_easy_setopt(curl, CURLOPT_URL, &url);
因为这会将std::string*
作为选项而不是char*
。正确的方法是
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());