在这里插入代码片
#ifndef IHTTPCLIENT_H
#define IHTTPCLIENT_H
#pragma once
#include <string>
#include <map>
using namespace std;
//struct UPFILE_IFAAS
//{
// string strImageUrl;
// string strFaceRect;
// string strCamreaCode;
// long lNodeID;
// string strKey;
// string strCid;
//};
//
//typedef UPFILE_IFAAS upload_ifaas;
#ifdef IHTTPCLIET_EXPORTS
#define IHTTPCLIET_API extern "C" __declspec(dllexport)
#else
#define IHTTPCLIET_API extern "C" __declspec(dllimport)
#endif
typedef void(*HttpCallBackData)(char * pinData, int pinLen, char * poutData, void * dwUser);
IHTTPCLIET_API int upload_file_ly(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len/*, string strImageUrl, string strFaceRect, string strCamreaCode, long lNodeID, string strKey, string strCid*/);
#if 1
class _declspec(dllexport) IHttpClient
{
public:
IHttpClient();
~IHttpClient();
/*************************************************
Function: initial
Description: IHttpClient 初始化,多个对象只需初始化一次
Return: 0:成功;非0:失败
*************************************************/
static int initial();
/*************************************************
Function: uninitial
Description: IHttpClient 反初始化,与initial配对使用
Return: 无
*************************************************/
static void uninitial();
static int progress_func(void *progress_data, double t, double d, double ultotal, double ulnow);
static size_t process_data(void *data, size_t size, size_t nmemb, std::string &content);
static size_t process_data1(void *data, size_t size, size_t nmemb, std::string &content);
/*************************************************
Function: post
Description: http的post发送json数据
url: http服务器的全路径
in_json_data: 发送的数据
out_resp: 服务器返回的数据内容
Return: 0:成功;-1:创建http失败;-2:发送数据失败;
*************************************************/
int post(const char* url, const char* in_json_data, char** out_resp, int* out_len);
/*************************************************
Function: upload_file
Description: 文件上传
url: http服务器的全路径
conten_type: http的conten_type,请参见http://tool.oschina.net/commons
filename: 上传的文件名
out_resp: 服务器返回的数据内容
map_parse: 上传文件附加的参数:key:参数名,value:参数值
Return: 0:成功;-1:创建http失败;-2:上传文件失败;
*************************************************/
int upload_file(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len, std::map<string, string> map_parse);
int upload_file_simpl(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len);
int upload_file_ifaas(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len, const char* strImageUrl, const char* strFaceRect, const char* strCamreaCode,long lNodeID , const char* strKey , const char* strCid);
/*************************************************
Function: down_file
Description: 文件下载
url: http服务器的全路径
filename: 上传的文件名
Return: 0:成功;-1:创建http失败;-2:下载的文件不存在;
*************************************************/
int down_file(const char* url, const char* filename);
int http_delete(char chJsonData);
void _delete(char *p);
private:
static bool m_bInitial; //false
const int FILE_EXIST; //200
};
#endif
#endif
#include "stdafx.h"
#include <curl/curl.h>
#include <sys/stat.h>
#include <json/json.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include "IHttpClient.h"
//#pragma comment(lib,"libcurld.lib")
//#pragma comment (lib, "lib_jsond.lib")
using namespace std;
bool IHttpClient::m_bInitial = false;
IHttpClient::IHttpClient()
:FILE_EXIST(200)
{
}
IHttpClient::~IHttpClient()
{
}
int IHttpClient::initial()
{
if (!m_bInitial)
{
CURLcode ret = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK == ret)
{
m_bInitial = true;
}
else
{
return -1;
}
}
return 0;
}
void IHttpClient::uninitial()
{
curl_global_cleanup();
}
size_t IHttpClient::process_data(void *data, size_t size, size_t nmemb, string &content)
{
long sizes = size * nmemb;
string temp;
temp = string((char*)data, sizes);
content += temp;
return sizes;
}
int IHttpClient::progress_func(void *progress_data, double t, double d, double ultotal, double ulnow)
{
//HttpClient *client = (HttpClient *)progress_data;
//client->progress_cb(ultotal, ulnow);
//printf("%s %g / %g (%g %%)\n", progress_data, d, t, d*100.0/t);
//printf("%f, %f, (%g %%)!\n", ulnow, ultotal, ulnow*100/ultotal);
return 0;
}
int IHttpClient::post(const char* url, const char* in_json_data, char** out_resp, int* out_len)
{
int n_ret = 0;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
CURL *curl = curl_easy_init();
if(!curl)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_formfree(formpost);
curl_slist_free_all (headerlist);
return n_ret;
}
string content;
CURLcode res;
// 设置超时时间为1秒
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Expect:");
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 3*1000);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 6*1000);
// 设置http发送的内容类型为JSON
//headerlist = curl_slist_append(headerlist, buf);
headerlist = curl_slist_append(headerlist, "Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &IHttpClient::process_data);
// 设置要POST的JSON数据
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, in_json_data);
//char *chTmp = "name=ly&type=test";
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, chTmp);
/* Perform the request, res will get the return code */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
n_ret = -2;
//fprintf(stderr, "is post json curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
cout << "is post json curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
else
{
long retcode = 0;
int iRet = 0;
CURLcode code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ((code == CURLE_OK) && retcode == 200 )
{
// 从 http response 获取文件远程地址
cout << "is post json response: " << content << endl;
int n_len = strlen(content.c_str());
*out_len = n_len;
char *p_resp = new char[n_len];
//*out_resp = new char[n_len];
memcpy(p_resp, content.c_str(), n_len);
*out_resp = p_resp;
}
}
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
return n_ret;
}
int IHttpClient::upload_file(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len, std::map<string, string> map_parse)
{
int n_ret = 0;
bool b_data = false;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
//static const char buf[] = "content-type:multipart/mixed;charset=gb2312";
static const char buf[] = "Expect:";
CURL *curl = curl_easy_init();
if (!curl)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_formfree(formpost);
curl_slist_free_all(headerlist);
return n_ret;
}
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/*struct curl_slist *head = NULL;
head = curl_slist_append(head, "Content-Type:application/x-www-form-urlencoded;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head);*/
/* Fill in the file upload field */
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filename, CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
//curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END);
//curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "robot_id", CURLFORM_COPYCONTENTS, "8938349", CURLFORM_END);
std::map<string, string>::iterator iter;
for (iter = map_parse.begin(); iter != map_parse.end(); iter++)
{
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, iter->first.c_str(), CURLFORM_COPYCONTENTS, iter->second.c_str(), CURLFORM_END);
}
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, url);
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
string content;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &IHttpClient::process_data);
// 设置链接超时
curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 ); //CURLOPT_TIMEOUT
// 获取上传进度
struct stat file_info;
/* get the file size of the local file */
stat(filename, &file_info);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &IHttpClient::progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
//n_ret = -2;
//fprintf(stderr, "upload curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
cout << "is post json curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
else
{
long retcode = 0;
int iRet = 0;
CURLcode code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (code == CURLE_OK) && retcode == 200 )
{
// 从 http response 获取文件远程地址
cout << "upload response: " << content << endl;
int n_len = strlen(content.c_str());
*out_len = n_len;
char *p_resp = new char[n_len];
//*out_resp = new char[n_len];
memcpy(p_resp, content.c_str(), n_len);
*out_resp = p_resp;
}
}
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
return n_ret;
}
int IHttpClient::upload_file_simpl(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len)
{
int n_ret = 0;
bool b_data = false;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
//static const char buf[] = "content-type:multipart/mixed;charset=gb2312";
static const char buf[] = "Expect:";
CURL *curl = curl_easy_init();
if (!curl)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_formfree(formpost);
curl_slist_free_all(headerlist);
return n_ret;
}
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/*struct curl_slist *head = NULL;
head = curl_slist_append(head, "Content-Type:application/x-www-form-urlencoded;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head);*/
/* Fill in the file upload field */
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filename, CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
//curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END);
//curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "robot_id", CURLFORM_COPYCONTENTS, "8938349", CURLFORM_END);
std::map<string, string>::iterator iter;
/*for (iter = map_parse.begin(); iter != map_parse.end(); iter++)
{
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, iter->first.c_str(), CURLFORM_COPYCONTENTS, iter->second.c_str(), CURLFORM_END);
}*/
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, url);
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
string content;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &IHttpClient::process_data);
// 设置链接超时
curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 ); //CURLOPT_TIMEOUT
// 获取上传进度
struct stat file_info;
/* get the file size of the local file */
stat(filename, &file_info);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &IHttpClient::progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
//n_ret = -2;
//fprintf(stderr, "upload curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
cout << "is post json curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
else
{
long retcode = 0;
int iRet = 0;
CURLcode code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (code == CURLE_OK) && retcode == 200 )
{
// 从 http response 获取文件远程地址
cout << "upload response: " << content << endl;
int n_len = strlen(content.c_str());
*out_len = n_len;
char *p_resp = new char[n_len];
//*out_resp = new char[n_len];
memcpy(p_resp, content.c_str(), n_len);
*out_resp = p_resp;
}
}
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
return n_ret;
}
int IHttpClient::upload_file_ifaas(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len, const char* strImageUrl, const char* strFaceRect, const char* strCamreaCode, long lNodeID, const char* strKey, const char* strCid)
{
int n_ret = 0;
bool b_data = false;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
char chNodeID[64] = {0};
sprintf(chNodeID, "%ld", lNodeID);
char tm[64];
SYSTEMTIME sys;
GetLocalTime(&sys);
sprintf(tm, "%4d-%02d-%02d %02d:%02d:%02d", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond);
/* Fill in the file upload field */
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filename, CURLFORM_END);
//curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "imageUrl", CURLFORM_COPYCONTENTS, strImageUrl, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "faceRect", CURLFORM_COPYCONTENTS, strFaceRect, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "cameraCode", CURLFORM_COPYCONTENTS, strCamreaCode, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "nodeId", CURLFORM_COPYCONTENTS, chNodeID, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, strKey, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "cid", CURLFORM_COPYCONTENTS, strCid, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "time", CURLFORM_COPYCONTENTS, tm, CURLFORM_END);
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
headerlist = curl_slist_append(headerlist, buf);
CURL *curl = curl_easy_init();
if (!curl)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_formfree(formpost);
curl_slist_free_all(headerlist);
return n_ret;
}
string content;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &IHttpClient::process_data);
// 设置链接超时
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); //CURLOPT_TIMEOUT
struct stat file_info;
/* get the file size of the local file */
stat(filename, &file_info);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &IHttpClient::progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
n_ret = -2;
//fprintf(stderr, "upload curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
string strerr = curl_easy_strerror(res);
cout << "is post json curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
else
{
long retcode = 0;
int iRet = 0;
CURLcode code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &retcode);
if ((code == CURLE_OK) && retcode == 200)
{
// 从 http response 获取文件远程地址
cout << "upload response: " << content << endl;
int n_len = strlen(content.c_str());
*out_len = n_len;
char *p_resp = new char[n_len];
//*out_resp = new char[n_len];
memcpy(p_resp, content.c_str(), n_len);
*out_resp = p_resp;
}
}
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all(headerlist);
return n_ret;
}
int IHttpClient::down_file(const char* url, const char* down_file_name)
{
int n_ret = 0;
// 初始化libcurl
CURLcode return_code;
// 获取easy handle
CURL *easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return n_ret;
}
// 设置easy handle属性
return_code = curl_easy_setopt(easy_handle, CURLOPT_URL, url);
//设置回调函数
return_code = curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
//回调函数的额外参数
std::string connectx;
return_code = curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &connectx);
// 执行数据请求
return_code = curl_easy_perform(easy_handle);
//判断获取响应的http地址是否存在,若存在则返回200,400以上则为不存在,一般不存在为404错误
int retcode = 0;
return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
if (CURLE_OK == return_code && FILE_EXIST == retcode)
{
double length = 0;
return_code = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length);
FILE *fp = fopen(down_file_name, "wb+");
fwrite(connectx.c_str(), 1, length, fp); //返回实际写入文本的长度,若不等于length则写文件发生错误.
fclose(fp);
}
else
{
cout << "is post json curl_easy_perform() failed: " << curl_easy_strerror(return_code) << endl;
std::cout << "请求的文件不存在!" << std::endl;
n_ret = -2;
return n_ret;
}
// 释放资源
curl_easy_cleanup(easy_handle);
return n_ret;
}
void IHttpClient::_delete(char *p)
{
if (NULL != p)
{
delete[] p;
p = NULL;
}
}
int upload_file_ly(const char* url, const char* conten_type, const char* filename, char** out_resp, int* out_len/*, string strImageUrl, string strFaceRect, string strCamreaCode, long lNodeID, string strKey, string strCid*/)
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
//curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, conten_type, CURLFORM_FILE, filename, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "imageUrl", CURLFORM_COPYCONTENTS, "isd", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "faceRect", CURLFORM_COPYCONTENTS, "vkfdk", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "cameraCode", CURLFORM_COPYCONTENTS, "44122500041321000001", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "nodeId", CURLFORM_COPYCONTENTS, "100000", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, "xxx", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "cid", CURLFORM_COPYCONTENTS, "", CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if (curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/fileUpload.action");
//if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
/* only disable 100-continue header if explicitly requested */
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all(headerlist);
}
return 0;
}
int IHttpClient::http_delete(char chJsonData)
{
int n_ret = 0;
CURL *curl = curl_easy_init();
if (NULL == curl)
{
cout << "get a easy handle failed." << endl;
n_ret = -1;
curl_easy_cleanup(curl);
curl_global_cleanup();
return n_ret;
}
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:9980/");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, chJsonData);
string content;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &IHttpClient::process_data);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "content-type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
CURLcode ret = curl_easy_perform(curl);
// do something...
ret = curl_easy_perform(curl);
/* Check for errors */
if (ret != CURLE_OK)
{
n_ret = -2;
//fprintf(stderr, "upload curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
cout << "is delete json curl_easy_perform() failed: " << curl_easy_strerror(ret) << endl;
}
else
{
long retcode = 0;
int iRet = 0;
CURLcode code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &retcode);
//if ((code == CURLE_OK) && retcode == 200)
//{
cout << "delete response: " << content << endl;
//}
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
/*
CURL *curl;
CURLcode res;
struct stat file_info;
double speed_upload, total_time;
FILE *fd;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "delete");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data1);
curl_easy_setopt(curl,CURLOPT_URL, "http://127.0.0.1:9980");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=huancong&password=doyou");
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
//cout << "receive data:" << endl << g_buf << endl;
}
curl_easy_cleanup(curl);
}
*/
return 0;
}
#if 0
int main()
{
IHttpClient::initial();
IHttpClient* m_HttpClient = new IHttpClient();
//char *chJsonData = "{\"test\" : \"123\"}";
char *chJsonData = "{\"path\":\".\"}";
char *chResp = NULL;
int nLen = 0;
m_HttpClient->post("http://127.0.0.1:9980", chJsonData, &chResp, &nLen);
//m_HttpClient->post("http://192.168.18.187:8088/api/list/", chJsonData, &chResp, &nLen);
cout << "main post json resp msg: " << chResp << "\n" << "main post json resp len: " << nLen << endl;
if (NULL != chResp)
{
delete[] chResp;
chResp = NULL;
}
//m_HttpClient->upload_file("http://192.168.18.233:7777/lua/savefile", "C:/Users/Public/Pictures/Sample Pictures/uptest.jpg", "cscsd4545", "uhhkkj7777");
//m_HttpClient->down_file("http://192.168.16.132:7778/download/room_58f0a8145fc547943c082081-XFaZDm6jZDplgc1DAAOV-2017080913571876.jpg", "download.jpg");
//m_HttpClient->upload_file("http://192.168.18.187:8088/api/upload/", "C:/Users/Public/Pictures/Sample Pictures/uptest.jpg", &chResp, &nLen);
//m_HttpClient->down_file("http://192.168.18.187:8088/static/68656164.png", "head.png");
IHttpClient::uninitial();
return 0;
}
#endif