libcurl库的简介(一)
一、Libcurl库简介
LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等协议,其主页是http://curl.haxx.se/。
- 具备线程安全
- 与IPv6兼容
二、libcurl库函数部分介绍
1. 调用curl_global_init()初始化libcurl
CURL_EXTERN CURLcode curl_global_init(long flags);
2. 调用 curl_easy_init()函数得到 easy interface型指针
CURL_EXTERN CURL *curl_easy_init(void);
3. 调用curl_easy_setopt设置传输选项,实现回调函数以完成用户特定任务
此函数详情请看:https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
BEHAVIOR OPTIONS:
CURLOPT_VERBOSE
Display verbose information. See CURLOPT_VERBOSE
CURLOPT_HEADER
Include the header in the body output. See CURLOPT_HEADER
CURLOPT_NOPROGRESS
Shut off the progress meter. See CURLOPT_NOPROGRESS
CURLOPT_NOSIGNAL
Do not install signal handlers. See CURLOPT_NOSIGNAL
CURLOPT_WILDCARDMATCH
Transfer multiple files according to a file name pattern. See CURLOPT_WILDCARDMATCH
CALLBACK OPTIONS:
CURLOPT_WRITEFUNCTION
Callback for writing data. See CURLOPT_WRITEFUNCTION
#include <curl/curl.h>
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
详细:https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
CURLOPT_WRITEDATA
Data pointer to pass to the write callback. See CURLOPT_WRITEDATA
eg:CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEDATA, void *pointer);
CURLOPT_READFUNCTION
Callback for reading data. See CURLOPT_READFUNCTION
size_t read_callback(char *buffer, size_t size, size_t nitems, void *userdata);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_READFUNCTION, read_callback);
CURLOPT_READDATA
Data pointer to pass to the read callback. See CURLOPT_READDATA
CURLOPT_IOCTLFUNCTION
Callback for I/O operations. See CURLOPT_IOCTLFUNCTION
CURLOPT_IOCTLDATA
Data pointer to pass to the I/O callback. See CURLOPT_IOCTLDATA
CURLOPT_SEEKFUNCTION
Callback for seek operations. See CURLOPT_SEEKFUNCTION
CURLOPT_SEEKDATA
Data pointer to pass to the seek callback. See CURLOPT_SEEKDATA
CURLOPT_SOCKOPTFUNCTION
Callback for sockopt operations. See CURLOPT_SOCKOPTFUNCTION
CURLOPT_SOCKOPTDATA
Data pointer to pass to the sockopt callback. See CURLOPT_SOCKOPTDATA
CURLOPT_OPENSOCKETFUNCTION
Callback for socket creation. See CURLOPT_OPENSOCKETFUNCTION
CURLOPT_OPENSOCKETDATA
Data pointer to pass to the open socket callback. See CURLOPT_OPENSOCKETDATA
CURLOPT_CLOSESOCKETFUNCTION
Callback for closing socket. See CURLOPT_CLOSESOCKETFUNCTION
CURLOPT_CLOSESOCKETDATA
Data pointer to pass to the close socket callback. See CURLOPT_CLOSESOCKETDATA
CURLOPT_PROGRESSFUNCTION
OBSOLETE callback for progress meter. See CURLOPT_PROGRESSFUNCTION
CURLOPT_PROGRESSDATA
Data pointer to pass to the progress meter callback. See CURLOPT_PROGRESSDATA
CURLOPT_XFERINFOFUNCTION
Callback for progress meter. See CURLOPT_XFERINFOFUNCTION
CURLOPT_XFERINFODATA
Data pointer to pass to the progress meter callback. See CURLOPT_XFERINFODATA
CURLOPT_HEADERFUNCTION
Callback for writing received headers. See CURLOPT_HEADERFUNCTION
CURLOPT_HEADERDATA
Data pointer to pass to the header callback. See CURLOPT_HEADERDATA
CURLOPT_DEBUGFUNCTION
Callback for debug information. See CURLOPT_DEBUGFUNCTION
CURLOPT_DEBUGDATA
Data pointer to pass to the debug callback. See CURLOPT_DEBUGDATA
CURLOPT_SSL_CTX_FUNCTION
Callback for SSL context logic. See CURLOPT_SSL_CTX_FUNCTION
CURLOPT_SSL_CTX_DATA
Data pointer to pass to the SSL context callback. See CURLOPT_SSL_CTX_DATA
CURLOPT_CONV_TO_NETWORK_FUNCTION
Callback for code base conversion. See CURLOPT_CONV_TO_NETWORK_FUNCTION
CURLOPT_CONV_FROM_NETWORK_FUNCTION
Callback for code base conversion. See CURLOPT_CONV_FROM_NETWORK_FUNCTION
CURLOPT_CONV_FROM_UTF8_FUNCTION
Callback for code base conversion. See CURLOPT_CONV_FROM_UTF8_FUNCTION
CURLOPT_INTERLEAVEFUNCTION
Callback for RTSP interleaved data. See CURLOPT_INTERLEAVEFUNCTION
CURLOPT_INTERLEAVEDATA
Data pointer to pass to the RTSP interleave callback. See CURLOPT_INTERLEAVEDATA
CURLOPT_CHUNK_BGN_FUNCTION
Callback for wildcard download start of chunk. See CURLOPT_CHUNK_BGN_FUNCTION
CURLOPT_CHUNK_END_FUNCTION
Callback for wildcard download end of chunk. See CURLOPT_CHUNK_END_FUNCTION
CURLOPT_CHUNK_DATA
Data pointer to pass to the chunk callbacks. See CURLOPT_CHUNK_DATA
CURLOPT_FNMATCH_FUNCTION
Callback for wildcard matching. See CURLOPT_FNMATCH_FUNCTION
CURLOPT_FNMATCH_DATA
Data pointer to pass to the wildcard matching callback. See CURLOPT_FNMATCH_DATA
CURLOPT_SUPPRESS_CONNECT_HEADERS
Suppress proxy CONNECT response headers from user callbacks. See CURLOPT_SUPPRESS_CONNECT_HEADERS
CURLOPT_RESOLVER_START_FUNCTION
Callback to be called before a new resolve request is started. See CURLOPT_RESOLVER_START_FUNCTION
CURLOPT_RESOLVER_START_DATA
Data pointer to pass to resolver start callback. See CURLOPT_RESOLVER_START_DATA
HTTP OPTIONS:
CURLOPT_AUTOREFERER
Automatically set Referer: header. See CURLOPT_AUTOREFERER
CURLOPT_ACCEPT_ENCODING
Accept-Encoding and automatic decompressing data. See CURLOPT_ACCEPT_ENCODING
CURLOPT_TRANSFER_ENCODING
Request Transfer-Encoding. See CURLOPT_TRANSFER_ENCODING
CURLOPT_FOLLOWLOCATION
Follow HTTP redirects. See CURLOPT_FOLLOWLOCATION
CURLOPT_UNRESTRICTED_AUTH
Do not restrict authentication to original host. CURLOPT_UNRESTRICTED_AUTH
CURLOPT_MAXREDIRS
Maximum number of redirects to follow. See CURLOPT_MAXREDIRS
CURLOPT_POSTREDIR
How to act on redirects after POST. See CURLOPT_POSTREDIR
CURLOPT_PUT
Issue an HTTP PUT request. See CURLOPT_PUT
CURLOPT_POST
Issue an HTTP POST request. See CURLOPT_POST
CURLOPT_POSTFIELDS
Send a POST with this data. See CURLOPT_POSTFIELDS
CURLOPT_POSTFIELDSIZE
The POST data is this big. See CURLOPT_POSTFIELDSIZE
CURLOPT_POSTFIELDSIZE_LARGE
The POST data is this big. See CURLOPT_POSTFIELDSIZE_LARGE
CURLOPT_COPYPOSTFIELDS
Send a POST with this data - and copy it. See CURLOPT_COPYPOSTFIELDS
CURLOPT_HTTPPOST
Multipart formpost HTTP POST. See CURLOPT_HTTPPOST
CURLOPT_REFERER
Referer: header. See CURLOPT_REFERER
CURLOPT_USERAGENT
User-Agent: header. See CURLOPT_USERAGENT
CURLOPT_HTTPHEADER
Custom HTTP headers. See CURLOPT_HTTPHEADER
CURLOPT_HEADEROPT
Control custom headers. See CURLOPT_HEADEROPT
CURLOPT_PROXYHEADER
Custom HTTP headers sent to proxy. See CURLOPT_PROXYHEADER
CURLOPT_HTTP200ALIASES
Alternative versions of 200 OK. See CURLOPT_HTTP200ALIASES
CURLOPT_COOKIE
Cookie(s) to send. See CURLOPT_COOKIE
CURLOPT_COOKIEFILE
File to read cookies from. See CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
File to write cookies to. See CURLOPT_COOKIEJAR
CURLOPT_COOKIESESSION
Start a new cookie session. See CURLOPT_COOKIESESSION
CURLOPT_COOKIELIST
Add or control cookies. See CURLOPT_COOKIELIST
CURLOPT_HTTPGET
Do an HTTP GET request. See CURLOPT_HTTPGET
CURLOPT_REQUEST_TARGET
Set the request target. CURLOPT_REQUEST_TARGET
CURLOPT_HTTP_VERSION
HTTP version to use. CURLOPT_HTTP_VERSION
CURLOPT_IGNORE_CONTENT_LENGTH
Ignore Content-Length. See CURLOPT_IGNORE_CONTENT_LENGTH
CURLOPT_HTTP_CONTENT_DECODING
Disable Content decoding. See CURLOPT_HTTP_CONTENT_DECODING
CURLOPT_HTTP_TRANSFER_DECODING
Disable Transfer decoding. See CURLOPT_HTTP_TRANSFER_DECODING
CURLOPT_EXPECT_100_TIMEOUT_MS
100-continue timeout. See CURLOPT_EXPECT_100_TIMEOUT_MS
CURLOPT_PIPEWAIT
Wait on connection to pipeline on it. See CURLOPT_PIPEWAIT
CURLOPT_STREAM_DEPENDS
This HTTP/2 stream depends on another. See CURLOPT_STREAM_DEPENDS
CURLOPT_STREAM_DEPENDS_E
This HTTP/2 stream depends on another exclusively. See CURLOPT_STREAM_DEPENDS_E
CURLOPT_STREAM_WEIGHT
Set this HTTP/2 stream's weight. See CURLOPT_STREAM_WEIGHT
4. 调用curl_easy_perform()函数完成传输任务
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);
5. 调用curl_easy_cleanup()释放内存
CURL_EXTERN void curl_easy_cleanup(CURL *curl);
实例:
#include <stdio.h>
#include <stdlib.h>
#include <curl\curl.h> FILE *fp = NULL;
int UTF8ToGBK(char const * strUTF8) ; size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
//int written = fwrite(ptr, size, nmemb, (FILE *)fp);
UTF8ToGBK((char *)ptr); //return written;
return 0;
} int UTF8ToGBK(char const * strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);
WCHAR* wszGBK = new WCHAR[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(LPCTSTR)strUTF8, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);
char *p_temp = szGBK;
int written = fwrite(p_temp, 1, strlen(p_temp), (FILE *)fp); delete[]szGBK;
delete[]wszGBK; return 0;
} int main(int argc, char *argv[])
{
//定义CURL类型的指针
CURL *p_curl = NULL;
//定义CURLcode类型的变量,保存返回状态码
CURLcode res; p_curl = curl_easy_init();
if (NULL == p_curl)
{
printf ("curl_easy_init() func error\n");
return -1;
} if (NULL == (fp = fopen("baidu", "wb")))
{
curl_easy_cleanup(p_curl);
return -1;
} curl_global_init(CURL_GLOBAL_ALL); //设置curl选项. 其中CURLOPT_URL是让用户指定url. argv[1]中存放的命令行传进来的网址
curl_easy_setopt(p_curl, CURLOPT_URL, "www.baidu.com");
curl_easy_setopt(p_curl, CURLOPT_WRITEFUNCTION, write_data);
//调用curl_easy_perform 执行我们的设置.并进行相关的操作. 在这里只在屏幕上显示出来.
res = curl_easy_perform(p_curl);
//清除curl操作.
curl_easy_cleanup(p_curl);
fclose(fp); return 0;
}
libcurl库的简介(一)的更多相关文章
- libcurl库的简介(二)
下面是使用libcurl库实现文件上传的一个实例: void CDataProcess::sendFileToServer(void) { string netIp = strNetUrl + &qu ...
- Linux之Libcurl库的介绍与应用20170509
一.LibCurl简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等 ...
- libcurl库进行http通讯-一些主要的函数
这里就简介一下libcurl的一些主要的函数. 调用curl_global_init()初始化libcurl 调用curl_easy_init()函数得到 easy interface型指针 调用cu ...
- C++ 用libcurl库进行http通讯网络编程
使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...
- C++ 用libcurl库进行http通讯网络编程(转)
转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...
- Cocos2d-x移植到WindowsPhone8移植问题-libcurl库移植问题
在Cocos2d-x 3.x最新版本中提供了Windows Phone 8平台移植libcurl库所需要的头文件和库文件.但要在Windows Phone 8平台成功移植libcurl库还是很不容易, ...
- Cocos开发中Visual Studio下libcurl库开发环境设置
我们介绍一下win32中Visual Studio下libcurl库开发环境设置.Cocos2d-x引擎其实已经带有为Win32下访问libcurl库,Cocos2d-x 3.x中libcurl库文件 ...
- C++ 用libcurl库进行http通讯网络编程[转]
http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...
- C/C++ 用libcurl库进行http通讯网络编程
C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...
随机推荐
- spring aop使用,spring aop注解,Spring切面编程
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
- Homebrew安装Mysql后的两步必要的命令
linxmousedeiMac:~ linxmouse$ mysql.server start Starting MySQL .. SUCCESS! linxmousedeiMac:~ linxmou ...
- 三、linux环境的搭建1(oracle、ssh、jdk、mysql、samba、tomcat)
linux环境的搭建1(oracle.ssh.jdk.mysql.samba.tomcat) 网络配置 方案一 tip 1 使用ifconfig : ifconfig eth0 新ip 然后编辑/ ...
- Java EE 7 API
学习Java必备资源,下载链接: https://pan.baidu.com/s/1P1xzuoGJCIuZlYBbPSbM_Q 提取码: dtui 复制这段内容后打开百度网盘手机App,操作更方便哦
- java注册界面及mysql连接
题目要求 完成注册界面及添加功能 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1 ...
- Ubuntu 的apt install 和卸载正确姿势
先说下使用apt installl 安装的包的卸载: apt-get remove: 卸载软件apt-get purge: 卸载软件和配置文件apt-get autoremove: 移除没有使用的依 ...
- Myeclipse的一些快捷键整理(转)
1. [ALT+/] 此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. ...
- PLSQL Developer12注册码
product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number:226959 password: xs374ca 绝对靠谱
- pyautocad
首部必须 #!/usr/bin/env python # -*- coding: utf-8 -*- 导入pyautocad import pyautocad import re # 设置python ...
- [AGC027A]Candy Distribution Again
Description AGC027A 你有一些糖果,你要把这些糖果一个不剩分给一些熊孩子,但是这帮熊孩子只要特定数目的糖果,否则就会不开心,求最多的开心人数. Solution 如果\(\sum a ...