-2

我正在开发一个程序,该程序将从 AZLyrics 等网站下载歌词。我正在使用 libcurl。
这是我的代码

歌词下载器.cpp

#include "lyricsDownloader.h"
#include <curl/curl.h>
#include <cstring>
#include <iostream>

#define DEBUG 1

/////////////////////////////////////////////////////////////////////////////


size_t lyricsDownloader::write_data_to_var(char *ptr, size_t size, size_t nmemb, void *userdata) // this function is a static member function
{
    ostringstream * stream = (ostringstream*) userdata;
    size_t count = size * nmemb;
    stream->write(ptr, count);
    return count;
}


string AZLyricsDownloader::toProviderCode() const
{ /*this creates an url*/ }

CURLcode AZLyricsDownloader::download()
{
    CURL * handle;
    CURLcode err;
    ostringstream buff;
    handle = curl_easy_init();
    if (! handle) return static_cast<CURLcode>(-1);
    // set verbose if debug on
    curl_easy_setopt( handle, CURLOPT_VERBOSE, DEBUG );
    curl_easy_setopt( handle, CURLOPT_URL, toProviderCode().c_str() ); // set the download url to the generated one
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buff);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &AZLyricsDownloader::write_data_to_var);
    err = curl_easy_perform(handle); // The segfault should be somewhere here - after calling the function but before it ends
    cerr << "cleanup\n";
    curl_easy_cleanup(handle);

    // copy the contents to text variable
    lyrics = buff.str();
    return err;
}

主文件

#include <QString>
#include <QTextEdit>
#include <iostream>
#include "lyricsDownloader.h"

int main(int argc, char *argv[])
{
        AZLyricsDownloader dl(argv[1], argv[2]);
        dl.perform();
        QTextEdit qtexted(QString::fromStdString(dl.lyrics));
        cout << qPrintable(qtexted.toPlainText());
        return 0;
}

跑步时

./maelyrica Anthrax Madhouse

我从 curl 得到这个记录

* About to connect() to azlyrics.com port 80 (#0)
*   Trying 174.142.163.250... * connected
* Connected to azlyrics.com (174.142.163.250) port 80 (#0)
> GET /lyrics/anthrax/madhouse.html HTTP/1.1
Host: azlyrics.com
Accept: */*

< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.0.12
< Date: Thu, 05 Jul 2012 16:59:21 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://www.azlyrics.com/lyrics/anthrax/madhouse.html
< 
Segmentation fault

奇怪的是,文件在那里。没有这样的页面时显示相同的错误(重定向到 azlyrics.com 主页)

我究竟做错了什么?

提前致谢

编辑:我将写入数据的函数设为静态,但这没有任何改变。甚至 wget 似乎也有问题

$ wget http://www.azlyrics.com/lyrics/anthrax/madhouse.html
--2012-07-06 10:36:05--  http://www.azlyrics.com/lyrics/anthrax/madhouse.html
Resolving www.azlyrics.com... 174.142.163.250
Connecting to www.azlyrics.com|174.142.163.250|:80... connected.
HTTP request sent, awaiting response... No data received.
Retrying.

为什么在浏览器中打开页面有效而 wget/curl 无效?

EDIT2:添加后:

curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);

并使功能静态一切正常。

4

1 回答 1

1

你的代码

    curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&AZLyricsDownloader::write_data_to_var);

以及来自 libcurl的文档中的以下引用

在连接 libcurl 时使用 C++ 而不是 C 时,基本上只需要记住一件事:

回调不能是非静态类成员函数

示例 C++ 代码:

class AClass { static size_t write_data(void *ptr, size_t size, size_t nmemb, void* ourpointer) { /* 对数据做你想做的事情 */ } }

可能是您的问题的根源,因为您的函数不是静态成员。即使不是,你也违反了这条规则。

这可能无法解决您的问题,但考虑到您在示例中发布的代码量,这是立即想到的第一件事,值得按照 libcurl 的建议进行更改。如果它不能解决您的问题,我建议您更详细地确定您遇到的错误,以便您下次可以提出更具体的问题(显示的代码要少得多)。

于 2012-07-05T17:18:15.497 回答