libcurl - 无法下载文件

时间:2012-07-05 17:03:30

标签: c++ curl download libcurl

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

lyricsDownloader.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;
}

的main.cpp

#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

我从卷曲中记录了这个

* 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);

让功能静止一切都没问题。

1 个答案:

答案 0 :(得分:1)

您的代码

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

以及documentation from libcurl

中的以下引用
  

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

     

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

     

示例C ++代码:

     

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

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

这可能无法解决您的问题,但考虑到您在示例中发布的代码量,这是第一个立即浮现在脑海中的内容,值得按照libcurl的建议进行更改。如果它没有解决您的问题,我建议您更详细地确定您所获得的错误,以便下次可以提出更具体的问题(显示更少的代码)。