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;
}

  

原文地址:https://www.cnblogs.com/Toney-01-22/p/9935026.html

时间: 2024-10-08 00:54:27

libcurl库的简介(一)的相关文章

libcurl库进行http通讯-开篇

从今天开始介绍一些常用的库. 首先就来写一写关于libcurl库. libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)., libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展),

muParser公式库使用简介( 转)

muParser是一个跨平台的公式解析库,它可以自定义多参数函数,自定义常量.变量及一元前缀.后缀操作符,二元操作符等,它将公式编译成字节码,所以计算起来非常快. 当前版本V1.28,官方网址http://sourceforge.net/projects/muparser/,这里是关于该库使用交流https://sourceforge.net/forum/forum.php?forum_id=462843 它提供两种方式使用,一种是将它编译进你的程序中,使用C++类,另一种是将它编译成共享库,可

Cocos2d-x移植到WindowsPhone8移植问题-libcurl库移植问题

在Cocos2d-x 3.x最新版本中提供了Windows Phone 8平台移植libcurl库所需要的头文件和库文件.但要在Windows Phone 8平台成功移植libcurl库还是很不容易,需要费一些周折,我总结了如下几个步骤:配置头文件搜索路径配置链接环境添加动态链接库 1.配置头文件搜索路径整个步骤我们可以参考上一节介绍的rapidjson库移植.具体过程是选中HelloCppComponent工程,打开菜单"项目"→ "属性"弹出工程属性对话框,选择

(转) c/c++调用libcurl库发送http请求的两种基本用法

libcurl主要提供了两种发送http请求的方式,分别是Easy interface方式和multi interface方式,前者是采用阻塞的方式发送单条数据,后者采用组合的方式可以一次性发送多条数据 一.Easy interface libcurl的easy interface是最基本的用法,简要流程为: 1.在主线程中调用curl_global_init(CURL_GLOBAL_ALL)初始化 2.调用curl_easy_init获取一个句柄: 3.调用curl_easy_setopt函数

libcurl库进行http通讯-一些基本的函数

这里就简单介绍一下libcurl的一些基本的函数. 调用curl_global_init()初始化libcurl 调用curl_easy_init()函数得到 easy interface型指针 调用curl_easy_setopt()设置传输选项 根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务 调用curl_easy_perform()函数完成传输任务 调用curl_easy_cleanup()释放内存 CURLcode curl_global_init

C++ 用libcurl库进行http通讯网络编程

http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.libcurl使用的HTTP消息头六.获取http应答头信息 七.多线程问题 八.什么时候libcurl无法正常工作 九.关于密码 十.HTTP验证 十一.代码示例 1.基本的ht

Cocos开发中Visual Studio下libcurl库开发环境设置

我们介绍一下win32中Visual Studio下libcurl库开发环境设置.Cocos2d-x引擎其实已经带有为Win32下访问libcurl库,Cocos2d-x 3.x中libcurl库文件所在位置是<工程目录>\cocos2d\external\curl\prebuilt\win32目录中,头文件在<工程目录>\cocos2d\external\curl\include\win32目录中.首先,我们需要配置头文件搜索路径,选中HelloCpp工程,打开菜单"项

mysql-数据库操作简介

1.创建数据库 是指在数据库系统中划分一块空间,用来存储相应数据 1.创建语法:CREATE DATABASE 数据库名 2.显示数据库:SHOW DATABASES; 2.删除数据库 1.语法:DROP DATABASE 数据库名; 3.数据库存储引擎(表类型) 1.显示mysql支持的存储引擎:SHOW ENGINES;或者SHOW ENGINES\g 2.显示mysql支持引擎:SHOW VARIABLES LIKE "hava%"; 3.查看mysql默认引擎:SHOW VAR

c用libcurl库实现https下get/post网络通信

一.LibCurl基本编程框架 libcurl是一个跨平台的网络协议库,支持http, https,ftp, gopher, telnet, dict, file, 和ldap 协议.libcurl同样支持HTTPS证书授权,HTTP POST,HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证.在基于LibCurl的程序里,主要采用callbackfunction (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时li