码迷,mamicode.com
首页 > Web开发 > 详细

libcurl库的简介(一)

时间:2018-11-09 16:25:46      阅读:28      评论:0      收藏:0      [点我收藏+]

标签:tar   content   proxy   tst   hunk   指针   dma   pat   ==   

一、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库的简介(一)

标签:tar   content   proxy   tst   hunk   指针   dma   pat   ==   

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

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!