博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
curl简单使用
阅读量:4613 次
发布时间:2019-06-09

本文共 4560 字,大约阅读时间需要 15 分钟。

首先,调用函数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, 
, 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
#include
#include
#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

转载于:https://www.cnblogs.com/mydomain/archive/2013/02/24/2924080.html

你可能感兴趣的文章
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>