curl简单使用

首先,调用函数curl_global_init()来初始化库函数;记得最后调用curl_global_cleanup()来释放库资源。其次,调用curl_easy_init()来初始化一个句柄,得到一个easy interface型指针;curl_easy_init函数是线程相关的,也就是说不能在一个线程中调用另外一个线程通过curl_easy_init创建的CURL指针。

记得最后要调用curl_easy_cleanup(easy interface);接着,再调用curl_easy_setopt来设置将要访问的网络地址。curl_easy_perform来执行下载。

注意的是:libcurl的全局初始化必须放在线程之外。

1、函数

CURLcode curl_easy_setopt(CURL *handle CURLoption option parameter);

CURLOPT_WRITEFUNCTION //设置回调函数

回调函数原型为: size_t function( void *ptrsize_t sizesize_t nmembvoid *userp);函数将在libcurl接收到数据后被调用。

void *ptr是下载回来的数据.

void *userp是用户指针,用户通过这个指针传输自己的数据。

CURLOPT_WRITEDATA

设置回调函数中的void *userp指针的来源。

CURLOPT_URL

设置访问的URI

CURLOPT_TIMEOUT

超时时间。

CURLOPT_CONNECTIONTIMEOUT

连接等待时间。

CURLOPT_RANGE

断点续传,指定传输分片,格式:"0-200"

示例代码1

View Code
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/ 
/* A multi-threaded example that uses pthreads extensively to fetch
 * X remote files at once */ 
 
#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>
 
#define NUMT 4
 
/*
  List of URLs to fetch.
 
  If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
  callback functions as described here:
 
  http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
 
*/ 
const char * const urls[NUMT]= {
  "http://curl.haxx.se/",
  "ftp://cool.haxx.se/",
  "http://www.contactor.se/",
  "www.haxx.se"
};
 
static void *pull_one_url(void *url)
{
  CURL *curl;
 
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_perform(curl); /* ignores error */ 
  curl_easy_cleanup(curl);
 
  return NULL;
}
 
 
/*
   int pthread_create(pthread_t *new_thread_ID,
   const pthread_attr_t *attr,
   void * (*start_func)(void *), void *arg);
*/ 
 
int main(int argc, char **argv)
{
  pthread_t tid[NUMT];
  int i;
  int error;
 
  /* Must initialize libcurl before any threads are started */ 
  curl_global_init(CURL_GLOBAL_ALL);
 
  for(i=0; i< NUMT; i++) {
    error = pthread_create(&tid[i],
                           NULL, /* default attributes please */ 
                           pull_one_url,
                           (void *)urls[i]);
    if(0 != error)
      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
    else
      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  }
 
  /* now wait for all threads to terminate */ 
  for(i=0; i< NUMT; i++) {
    error = pthread_join(tid[i], NULL);
    fprintf(stderr, "Thread %d terminated\n", i);
  }
 
  return 0;
}

示例代码2

View Code
获取网站包括HTTP头部信息在内的500字节数据
size_t callback_get_head(void *ptr, size_t size, size_t nmemb, void *userp)
 {
    strcat( userp, ptr);
    return size * nmemb;     //必须返回这个大小, 否则只回调一次
 }

 void *get_head_thread(void *)
 {
    CURL *curl = curl_easy_init();
    
    curl_easy_setopt(curl, CURLOPT_URL, "www.163.com"); //设置下载的URI
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20);        //设置超时
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);        //屏蔽其它信号
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);          //下载数据包括HTTP头部
    curl_easy_setopt(curl, CURLOPT_RANGE, "0-500");     //用于断点续传, 设置下载的分片
    
    char buffer[MAXHEADLEN] = {0x0};
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_get_head); //设置下载数据的回调函数
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);  

    curl_easy_perform(curl);  
    curl_easy_cleanup(curl);

    //此时网站HTTP头信息已经存放在buffer内.
 }

 

参考

[1]http://curl.haxx.se/libcurl/c/multithread.html

[2]http://blog.csdn.net/aaa20090987/article/details/7955918

[3]http://hi.baidu.com/445920201/item/99401f1674bf4b5e2a3e22b4

[4]http://blog.chinaunix.net/uid-20692625-id-3203258.html

posted @ 2013-02-24 12:02  浪里飞  阅读(2639)  评论(0编辑  收藏  举报