C++CURL无法正确检索网页
C++ CURL not retrieving webpage properly
我的类中有以下三个方法-
void WebCrawler::crawl()
{
urlQueue.push("http://www.google.com/");
if(!urlQueue.empty())
{
std::string url = urlQueue.front();
urlQueue.pop();
pastURLs.push_back(url);
if(pastURLs.size()>4000000)
{
pastURLs.erase(pastURLs.begin());
}
std::string data=getData(url);
auto newPair= std::pair<std::string, std::string>(url, data);
dataQueue.push(newPair);
}
}
std::string WebCrawler::getData(std::string URL)
{
std::string readBuffer = "";
CURL *curl = curl_easy_init();
if(curl)
{
CURLcode res;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WebCrawler::WiteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return readBuffer;
}
和
size_t WebCrawler::WiteCallback(char* buf, size_t size, size_t nmemb, void* up)
{
((std::string*)up)->append((char*)buf, size * nmemb);
return size * nmemb;
}
当我把这些方法从类中取出并作为函数运行时,我的代码会正确执行并返回网页内容。然而,一旦我将这些方法放入我的类中,它们的行为就会开始不同。当调用我的WriteCallback时,程序失败,并表示无法分配45457340335435776字节的数据。我有点困惑是什么导致了这种变化,任何帮助都将不胜感激。
WebCrawler::WiteCallback
是一个非静态方法,这意味着需要传递指向对象(this
)的指针。根据ABI的不同,它可以是一个隐式参数,一个不用于正常参数传递的寄存器,或者其他任何东西。对于ABI,看起来对象是作为最左边的参数("(WebCrawler *this, char* buf, size_t size, size_t nmemb, void* up)
")传递的。
你不能那样做。要么使WebCrawler::WiteCallback
静止,要么使用蹦床:
size_t WebCrawler::WriteCallbackTramp(char* buf, size_t size,
size_t nmemb, void* up)
{
return ((WebCrawler*) up)->WriteCallback(buf, size, nmemb);
}
其中CCD_ 5包含用于缓冲器的成员。
将方法设置为静态是更好的解决方案。
维基百科:调用约定
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 是否可以从格式字符串中检索"width"
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- 在 emscripten 网页汇编正在运行期间更新进度条?
- 如何在Qt中从数据库中检索二进制数据?
- 如何从C++代码中检索 QML 的文本字段中的文本?
- 从 opencv c++ 中的矢量中检索固定的帧数
- 如何在不等待检索的情况下获取C++中的内存位置?
- 如何在 QTreeWidget 中检索特定项目的 mimeData?
- C++从字符串中检索几行
- 如何使用QT创建器获取网页源代码?
- 在数据库中插入和检索矩阵
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 使用 const char* 键映射 C++ 检索空值
- 打印从SQLite3数据库检索的数据C++与命令行中相同的方式
- 如何检索指向在单独线程上运行的函数的移动指针?
- 如何检索 Steamwork SDK 的当前版本
- 从网页中检索特定信息
- C++CURL无法正确检索网页