简要了解
- LibCurl
LibCurl是一个利用URL语法在命令行方式下工作的文件传输工具。支持众多的协议,如 FTP, HTTP, HTTPS, IMAP, POP3, SMTP, TELNET 等,同时,libcurl 支持 SSL 验证,基于 HTTP 协议的文件上传,代理,客户端缓冲等。使用 libcurl,可以很轻松的实现一个简单的 WEB 浏览器,或者一个 FTP 的客户端应用,邮件客户端等。 - LibCurl编程
①编程流程
LibCurl编程流程在基于LibCurl的程序里,主要采用callback function (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时libcurl将调用用户的回调函数实现特定功能。
下面是利用libcurl完成传输任务的流程:
(1)调用curl_global_init()初始化libcurl
(2)调用 curl_easy_init()函数得到 easy interface型指针
(3)调用curl_easy_setopt设置传输选项
(4)根据curl_easy_setopt设置的传输选项,实现回调函数以完成用户特定任务
(5)调用curl_easy_perform()函数完成传输任务
(6)调用curl_easy_cleanup()释放内存
在整过过程中设置curl_easy_setopt()参数是最关键的,几乎所有的libcurl程序都要使用它。
②基本函数
(1) CURLcode curl_global_init(long flags);
这个函数只能用一次,但其实在调用curl_global_cleanup 函数后仍然可再用。如果这个函数在curl_easy_init函数调用时还没调用,它将由libcurl库自动完成。
(2) void curl_global_cleanup(void);
在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数。
(3) char curl_version( );
打印当前libcurl库的版本。
(4) CURL curl_easy_init( );
用来初始化一个CURL的指针(有些像返回FILE类型的指针一样).一般意味着一个会话的开始. 它的返回值一般都用在easy系列的函数中.
(5) void curl_easy_cleanup(CURL handle);
用来结束一个会话.与curl_easy_init配合着用.
(6) CURLcode curl_easy_setopt(CURL handle, CURLoption option, parameter);
这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.它告诉curl库.程序将有如何的行为
(7) CURLcode curl_easy_perform(CURL handle);
在初始化CURL类型的指针 以及curl_easy_setopt完成后调用。
编程例子
以下例子基于ubuntu 16.04下使用curl
使用命令sudo apt-get install curl即可轻松安装curl
- 基本的http GET/POST操作
创建文件1.c,实现get/post操作,把文件保存在当前目录下并分别命名为get.html post.html
#include <stdio.h>
#include <curl/curl.h>
int getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回结果用文件存储
return 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头输出到fp指向的文件
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的html主体数据输出到fp指向的文件
res = curl_easy_perform(curl); // 执行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return 1;
}
}
int postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
return 0;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "./cookie.txt"); // 指定cookie文件
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post内容
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
curl_easy_setopt(curl, CURLOPT_URL, " http://mail.sina.com.cn/cgi-bin/login.cgi "); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return 1;
}
int main(void)
{
getUrl("./get.html");
postUrl("./post.html");
}
gcc编译运行后查看结果
- 获取HTML网页、网页下载保存
①获取网页
创建get_html.c文件:
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
CURL *curl; //定义CURL类型的指针
CURLcode res; //定义CURLcode类型的变量,保存返回状态码
if(argc!=2)
{
printf("Usage : file <url>;\n");
exit(1);
}
curl = curl_easy_init(); //初始化一个CURL类型的指针
if(curl!=NULL)
{
//设置curl选项. 其中CURLOPT_URL是让用户指 定url. argv[1]中存放的命令行传进来的网址
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
//调用curl_easy_perform 执行我们的设置.并进行相关的操作. 在这 里只在屏幕上显示出来.
res = curl_easy_perform(curl);
//清除curl操作.
curl_easy_cleanup(curl);
}
return 0;
}
gcc编译文件,使用命令运行./get_html www.baidu.com,查看获取百度网页的结果
②下载网页
创建down_html.c文件
// 采用CURLOPT_WRITEFUNCTION 实现网页下载保存功能
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
FILE *fp; //定义FILE类型指针
//这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的
//完成数据保存功能
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr, size, nmemb, (FILE *)fp);
return written;
}
int main(int argc, char *argv[])
{
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl=curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
if((fp=fopen(argv[2],"w"))==NULL)
{
curl_easy_cleanup(curl);
exit(1);
}
CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
exit(0);
}
gcc编译后,将下载的百度网页保存到当前文件夹命名为baidu
- 文件的断点续传
创建文件resume.c,下载华军软件园里的winrar 5.91官方版 这一文件
//采用CURLOPT_RESUME_FROM_LARGE 实现文件断点续传功能
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <curl/curl.h>
//这个函数为CURLOPT_HEADERFUNCTION参数构造
/* 从http头部获取文件size*/
size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {
int r;
long len = 0;
/* _snscanf() is Win32 specific */
// r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
r = sscanf(ptr, "Content-Length: %ld\n", &len);
if (r) /* Microsoft: we don't read the specs */
*((long *) stream) = len;
return size * nmemb;
}
/* 保存下载文件 */
size_t wirtefunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
/*读取上传文件 */
size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
FILE *f = stream;
size_t n;
if (ferror(f))
return CURL_READFUNC_ABORT;
n = fread(ptr, size, nmemb, f) * size;
return n;
}
// 下载 或者上传文件函数
int download(CURL *curlhandle, const char * remotepath, const char * localpath,
long timeout, long tries)
{
FILE *f;
curl_off_t local_file_len = -1 ;
long filesize =0 ;
CURLcode r = CURLE_GOT_NOTHING;
int c;
struct stat file_info;
int use_resume = 0;
/* 得到本地文件大小 */
//if(access(localpath,F_OK) ==0)
if(stat(localpath, &file_info) == 0)
{
local_file_len = file_info.st_size;
use_resume = 1;
}
//采用追加方式打开文件,便于实现文件断点续传工作
f = fopen(localpath, "ab+");
if (f == NULL) {
perror(NULL);
return 0;
}
//curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout); // 设置连接超时,单位秒
//设置http 头部处理函数
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);
// 设置文件续传的位置给libcurl
curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, wirtefunc);
//curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
//curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
r = curl_easy_perform(curlhandle);
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n", curl_easy_strerror(r));
return 0;
}
}
int main(int c, char **argv) {
CURL *curlhandle = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curlhandle = curl_easy_init();
//download(curlhandle, "ftp://user:pass@host/path/file", "C:\\file", 0, 3);
download(curlhandle , "https://www.onlinedown.net/soft/5.htm","./index.asp",1,3);
curl_easy_cleanup(curlhandle);
curl_global_cleanup();
return 0;
}
总结+参考
libcurl用法还有很多,还需要继续学习。
Ubuntu 16下curl的使用
libcurl编程学习