woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

libcurl应用:如何把下载内容写入内存

libcurl的文档中有getinmemory.c这个例子,把下载的网页写入自己定义的一个memorystruct中,看了这个例子的做法,它需要自己照顾内存的开辟,特别是需要在curl_easy_perform的调用函数中释放已开辟的内存,很不利于函数的封装,而且应用于multihandle的时候,运行时会出莫名其妙的assert错误。因此改写了一下,将下载的内容放入stlstring中,这样根本不需要自己照顾内存的开辟和释放。源代码如下:

#include <string>
#include "curl.h"

using namespace std;
static char errorBuffer[CURL_ERROR_SIZE];
static int writer(char *, size_t, size_t, string *);
static bool init(CURL *&, char *,string *);

int main()
{
    CURL *conn = NULL;
    CURLcode code;
string buffer;
 
    curl_global_init(CURL_GLOBAL_DEFAULT);
    char* url="http://cool.haxx.se/";
if (!init(conn,url,&buffer ))
    {
        fprintf(stderr, "Connection initializion failed\n");
        exit(EXIT_FAILURE);
    }
    code = curl_easy_perform(conn);
  
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to get '%s' [%s]\n", url, errorBuffer);
        exit(EXIT_FAILURE);
    }
curl_easy_cleanup(conn);
printf("%s\n",buffer.c_str());
    return 0;
   
}

static bool init(CURL *&conn, char *url,string *p_buffer)
{
    CURLcode code;
    conn = curl_easy_init();
    if (conn == NULL)
    {
        fprintf(stderr, "Failed to create CURL connection\n");
        exit(EXIT_FAILURE);
    }
    code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to set error buffer [%d]\n", code);
        return false;
    }
    code = curl_easy_setopt(conn, CURLOPT_URL, url);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
        return false;
    }
    code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
        return false;
    }
    code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
        return false;
    }
    code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, p_buffer);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
        return false;
    }
    return true;
}

static int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
    unsigned long sizes = size * nmemb;
    if (writerData == NULL) return 0;
    writerData->append(data, sizes);
    return sizes;
}

 

posted on 2009-03-13 12:07 肥仔 阅读(3447) 评论(-2)  编辑 收藏 引用 所属分类: 网络编程

评论

# re: libcurl应用:如何把下载内容写入内存  回复  更多评论   

I received my first <a href="http://bestfinance-blog.com/topics/mortgage-loans">mortgage loans</a> when I was 25 and it aided me very much. However, I require the credit loan also.
2010-11-26 20:54 | BurgessDEBORAH

# re: libcurl应用:如何把下载内容写入内存  回复  更多评论   

To order thesis statement supposes to be simple. Nevertheless, good students must remember that only a trustworthy dissertation writing service can provide good quality.
2011-06-23 06:27 | dissertation writing service

# re: libcurl应用:如何把下载内容写入内存  回复  更多评论   

Writing companies with many years of experience in producing of different sorts of resume papers will be good advisers for the people who need to order resume of the high quality.
2013-08-19 11:42 | over here

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理