libcurl - 无法下载文件 [英] libcurl - unable to download a file

查看:198
本文介绍了libcurl - 无法下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个程序,将从网站如AZLyrics下载歌词。我使用libcurl。 $ b $bÚ
这是我的代码

I'm working on a program which will download lyrics from sites like AZLyrics. I'm using libcurl.
It's my code

lyricsDownloader.cpp

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

from curl

I'm getting this logged from 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主页)时显示相同的错误

Strangely, the file is there. The same error is displayed when there's no such page (redirect to azlyrics.com mainpage)

我做错了什么?

提前感谢

编辑:我写了静态数据的函数,但这没有什么改变。
即使wget似乎有问题

I made the function for writing data static, but this changes nothing. Even wget seems to have problems

$ 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不?

Why does opening the page in a browser work and wget/curl not?

EDIT2:添加此项后:

After adding this:

curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);

使函数静态一切正常。

推荐答案

您的代码

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

,并从文档


当连接libcurl时使用C ++
而不是C:

There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl:

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

The callbacks CANNOT be non-static class member functions

示例C ++代码:

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

class AClass { static size_t write_data(void *ptr, size_t size, size_t nmemb, void* ourpointer) { /* do what you want with the data */ } }

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

could be the source of your problem as your function is not a static member. Even if not you are breaking this rule.

这可能无法解决你的问题,但考虑到你在你的例子中发布的代码量,这是第一件立即想到这是值得改变这是libcurl推荐。如果它不能解决你的问题,我建议更多的细节,以便你可以提出一个更具体的问题,更多的具体问题,显示更少的代码(显示更少的代码)。

This may not solve your problem but given the amount of code you have posted in your example, that was the first thing that immediately came to mind and it is worth changing this as recommended by libcurl. If it does not solve your problem I would suggest identifying the error you are getting in more detail so that you can pose a more specific question next time (with a lot less code displayed).

这篇关于libcurl - 无法下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆