标题cmake的编译:先config,然后generate,最后打开工程
http服务器工具 :HFS
测试代码如下:
#include <iostream>
#include "curl/curl.h"
using namespace std;
#define CHECK_FORM_ERROR(x) \
if ((formCode = (x)) != CURL_FORMADD_OK) \
{ \
fprintf(stderr, "curl_formadd[%d] error.\n", formCode); \
goto out; \
}
#define CHECK_SETOPT_ERROR(x) \
if ((code = (x)) != CURLE_OK) \
{ \
fprintf(stderr, "curl_easy_setopt[%d] error.\n", code); \
goto all; \
}
int http_post_file(const char *url, const char *user, const char *pwd, const char *filename)
{
int ret = -1;
CURL *curl = NULL;
CURLcode code;
CURLFORMcode formCode;
int timeout = 15;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
struct curl_slist *headerlist = NULL;
CHECK_FORM_ERROR(curl_formadd(&post, &last, CURLFORM_COPYNAME, "user",
CURLFORM_COPYCONTENTS, user,
CURLFORM_END));
CHECK_FORM_ERROR(curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
CURLFORM_COPYCONTENTS, pwd,
CURLFORM_END));
CHECK_FORM_ERROR(curl_formadd(&post, &last, CURLFORM_COPYNAME, "file",
CURLFORM_FILE, filename,
CURLFORM_END));
CHECK_FORM_ERROR(curl_formadd(&post, &last,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "upload",
CURLFORM_END));
curl = curl_easy_init();
if (curl == NULL)
{
fprintf(stderr, "curl_easy_init() error.\n");
goto out;
}
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HEADER, 0));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_URL, url));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HTTPPOST, post));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout));
code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform[%d] error.\n", code);
goto all;
}
ret = 0;
all:
curl_easy_cleanup(curl);
out:
curl_formfree(post);
return ret;
}
/*表单上传内存数据*/
int http_post_buf(const char *url, const char *user, const char *pwd, const char *pBody, long len)
{
int ret = -1;
CURL *curl = NULL;
CURLcode code;
CURLFORMcode formCode;
int timeout = 15;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
struct curl_slist *headerlist = NULL;
CHECK_FORM_ERROR(curl_formadd(&post, &last, CURLFORM_COPYNAME, "user",
CURLFORM_COPYCONTENTS, user,
CURLFORM_END));
CHECK_FORM_ERROR(curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
CURLFORM_COPYCONTENTS, pwd,
CURLFORM_END));
CHECK_FORM_ERROR(curl_formadd(&post, &last,
CURLFORM_COPYNAME, "file",
CURLFORM_BUFFER, "unnamed.jpg",
CURLFORM_BUFFERPTR, pBody,
CURLFORM_BUFFERLENGTH, len,
CURLFORM_CONTENTTYPE, "image/jpg",
CURLFORM_END));
curl = curl_easy_init();
if (curl == NULL)
{
fprintf(stderr, "curl_easy_init() error.\n");
goto out;
}
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HEADER, 0));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_URL, url));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HTTPPOST, post));
CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout));
code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform[%d] error.\n", code);
goto all;
}
ret = 0;
all:
curl_easy_cleanup(curl);
out:
curl_formfree(post);
return ret;
}
int main(int argc, char* argv[]) {
//http_post_file("http://10.73.0.186:12345/HTTP/", "admin", "admin123", "./test.jpg");
http_post_buf("http://10.73.0.186:12345/HTTP/", "admin", "admin123", "21324", 5);
return 0;
}
int HttpPostFile(char *pPath, char *szUrl, char *pAppId, char *pSecret)
{
char *pFileName = NULL;
char *pTmpStr = strrchr(pPath, '/');
if (pTmpStr == NULL)
{
pFileName = pPath;
}
else
{
pFileName = pTmpStr + 1;
}
if (NULL == szUrl || NULL == pAppId || NULL == pSecret || NULL == pPath)
{
return -1;
}
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
curl_formadd(&post, &last, CURLFORM_COPYNAME, "file",
CURLFORM_FILE, pFileName,
CURLFORM_END);
CURL *curl = curl_easy_init();
if (curl == NULL)
{
fprintf(stderr, "curl_easy_init() error.\n");
goto out;
}
int nCurTime = 1645673533;
char szCurTimeHeader[256] = { 0 };
sprintf(szCurTimeHeader, "ts: %d", nCurTime);
char szInput[256] = { 0 };
sprintf(szInput, "%s%s%lld", pAppId, pSecret, nCurTime);
char szMd5[256] = { 0 };
md5_ops_str_calc(szInput, strlen(szInput), szMd5);
char szAuthHeader[256] = { 0 };
sprintf(szAuthHeader, "Authorization: Bearer %s", szMd5);
char szAppIdHeader[256] = { 0 };
sprintf(szAppIdHeader, "appId: %s", pAppId);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Connection: keep-alive");
headers = curl_slist_append(headers, szAuthHeader);
headers = curl_slist_append(headers, szAppIdHeader);
headers = curl_slist_append(headers, szCurTimeHeader);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, szUrl);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 4);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 4);
CURLcode code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform[%d] error.\n", code);
goto all;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
// 释放表单
curl_formfree(post);
return code;
all:
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_formfree(post);
return -1;
out:
curl_formfree(post);
return -1;
}