libcurl库的使用支持http、https、ftp(上传、下载、远端目录列表获取)

本文详细介绍了如何使用libcurl库进行HTTP、HTTPS和FTP的调用,包括上传、下载和获取远程目录列表。通过示例代码展示了libcurl的功能和使用方法。
摘要由CSDN通过智能技术生成

http调用如图:

FTP调用如图:

FTP效果如图:

头文件:

#pragma once
#include <string>
#include <vector>
#include "include/curl.h"
#include "include/easy.h"
#pragma comment(lib,"curl/lib/libcurl.lib")
 
/************************************************************************/
/*           libcurl库封装    ssdwujianhua 2017年6月7日 13:17:11      */
/************************************************************************/
 
//表单key对应的类型
enum E_KEY_TYPE {
	e_key_text,			//文本类型
	e_key_iamge			//图片类型
};
 
//表单信息结构
typedef struct 
{
	std::string strKey;
	std::string strVal;
	E_KEY_TYPE eKeyType;
 
	void Set(std::string key, std::string val, E_KEY_TYPE eType)
	{
		strKey = key;
		strVal = val;
		eKeyType = eType;
	}
}POST_LIST, *LPPOST_LIST;
 
 
//表单数据
#define  _POST_LIST_DATA_ std::vector<POST_LIST>
 
class CTools
{
public:
	static std::string replace(const char *pszSrc, const char *pszOld, const char *pszNew);
	static const char * getAppPath();
};
 
class CUrlHttp
{
public:
	CUrlHttp(void);
	~CUrlHttp(void);
	static int Request(std::string strRequestType,
		std::string strUrl,
		std::string &strReport,		
		std::vector<std::string> vecHeader,
		std::string strParam="", 
		std::string strCookie="",  
		std::string strCaPath="",
		int nTimeOut=0);
 
	//有图片建议使用表单提交比较方便
	static int RequestSSL(std::string strUrl,
		std::string &strReport,
		_POST_LIST_DATA_ listParam, 
		std::vector<std::string> vecHeader,
		std::string strCookie="",  
		std::string strCaPath="",
		int nTimeOut=0);
};
 
class CUrlFtp
{
public:
	CUrlFtp();
	~CUrlFtp();
	typedef struct 
	{
		size_t type;					//0:文件夹 1:文件
		std::string name;				//名称
		std::string permissions;		//权限
	}FILE_INFO, *LPFILE_INFO;
	
public:
	int connect(const char *user, const char *password, const char * ip, short port=21);
	void close();
	int download(const char * remoteFile, const char * localFile, size_t timeOut=0);
	int upload(const char * remoteFile, const char * localFile, size_t timeOut=0);
	int dirlist(const char * remote, std::vector<FILE_INFO> &vecFileInfo);
	const char * getLastError();
 
private:
		CURL *curl;
		std::string m_ip;
		std::string m_user;
		std::string m_password;
		short m_port;
		std::string m_lastError;
};

源文件:


#include "curltools.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
 
//参数文档地址:https://curl.haxx.se/libcurl/c/libcurl-tutorial.html
 
namespace _CURL_
{  
	/**
	* buf: 从服务器返回的buffer
	* unit: buufer的单位
	* bufSize: buffer的大小
	* data: 保存从服务器返回的内容
	* 注意这个函数会被调用多次
	*/
	static size_t write_data(void *buf, size_t unit, size_t bufSize, std::string * data) 
	{
		int size = unit * bufSize;
		char * tmp = (char*)malloc(size + 1);
		memcpy(tmp, buf, size);
		tmp[size] = '\0';
		data->append(tmp);
		free(tmp);
		return size;
	}
 
	static size_t ftp_read(void *ptr, size_t size, size_t nmemb, void *stream)
	{
		curl_off_t nread;
		size_t retcode = fread(ptr, size, nmemb, (FILE *)stream);
		nread = (curl_off_t)retcode;
		return retcode;
	}
 
	//ftp 文件结构
	typedef struct FtpFile 
	{
		char filename[512];					//文件名称
		FILE *stream;						//文件操作指针
	}FTP_FILE, *LPFTP_FILE;
 
	static size_t ftp_write(void *buffer, size_t size, size_t nmemb,void *stream)
	{
		struct FtpFile *out = (struct FtpFile *)stream;
		if(out && !out->stream) {
			out->stream = fopen(out->filename, "wb");
			if(!out->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值