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