C++ libcurl-无法下载文件
我正在开发一个程序,可以从AzLyps等网站下载歌词。我用的是libcurl。C++ libcurl-无法下载文件,c++,curl,download,libcurl,C++,Curl,Download,Libcurl,我正在开发一个程序,可以从AzLyps等网站下载歌词。我用的是libcurl。 这是我的代码 lyricsDownloader.cpp #include "lyricsDownloader.h" #include <curl/curl.h> #include <cstring> #include <iostream> #define DEBUG 1 /////////////////////////////////////////////////////
这是我的代码
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;
}
我从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
为什么在浏览器中打开页面有效而wget/curl无效
EDIT2:添加此项后:
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
使函数保持静态一切正常。您的代码
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&AZLyricsDownloader::write_data_to_var);
以下引自
当使用C++时,基本上只有一件事需要牢记。
在连接libcurl时使用C代替:
回调不能是非静态类成员函数
这可能无法解决您的问题,但考虑到您在示例中发布的代码数量,这是第一件立即想到的事情,值得按照libcurl的建议进行更改。如果它不能解决您的问题,我建议您更详细地识别错误,以便下次可以提出更具体的问题(显示的代码要少得多)。在这里发布之前,您应该能够缩小seg故障的来源,并将代码减少到相关部分。用“我做错了什么?”来发布所有代码不是正确的方法。在gdb这样的调试器中运行代码,应该可以非常容易地找到seg故障的位置。难以在“代码墙”中筛选一个简短的自包含编译示例最好将问题集中在您遇到的特定问题上。此外,通过举一个更狭隘的例子,你有时可以自己解决这个问题!好的,对不起,我是新来的:)我减少了code@marmistrz哦,好吧,照libcurl说的做仍然是件好事。可能会防止将来出现其他问题。编辑:问题确实是非静态函数与libcurl相结合,没有遵循重定向。谢谢
$ 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.
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&AZLyricsDownloader::write_data_to_var);