1.路径无中文
std::string respBodyData;
std::string respHeadData;
CURL* curl;
CURLcode res;
Logger::HttpInfo(constant, "无请求参数");
struct curl_slist *headers = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl == NULL)
{
MessageBox(NULL, L"错误的网络连接!", L"错误", MB_ICONERROR);
return S_FALSE;
}
std::string url;
ReadContantFromFile("addr", url, GetCredentialsFilePath);
FILE *file = fopen(filePath.c_str(), "rb");
if (!file) {
MessageBox(NULL, L"文件打开失败,请联系管理员!", L"错误", MB_ICONERROR);
return S_FALSE;
}
size_t pos = filePath.rfind('\\');
std::string fileNameGbk = filePath.substr(pos + 1);
std::string fileNameUtf8;
GbkToUtf8(fileNameGbk, fileNameUtf8);
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "files");
ULONGLONG fileSize = GetFileSize(filePath);
curl_mime_filename(part, filePath.c_str());
curl_mime_filename(part, fileNameUtf8.c_str());
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, (url + mapping).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WritePostHeaderResp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WritePostBodyResp);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &respHeadData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respBodyData);
bool bCA = FALSE;
if (!bCA)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_easy_setopt(curl, CURLOPT_CAINFO, "");
}
res = curl_easy_perform(curl);
fclose(file);
if (res != CURLE_OK)
{
MessageBox(NULL, CA2W(curl_easy_strerror(res)), L"错误", MB_ICONEXCLAMATION);
return S_FALSE;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
std::string respBodyDataGbk;
Utf8ToGbk(respBodyData, respBodyDataGbk);
std::string result = respBodyDataGbk.c_str();
Logger::HttpInfo(constant, respBodyDataGbk, false);
return ResultHandler(result, data);
2.路径有中文
size_t ReadCallback(char *buffer, size_t size, size_t nitems, void *userdata) {
FILE *file = (FILE *)userdata;
return fread(buffer, size, nitems, file);;
}
int HttpPost(const std::string constant, const char * mapping, Json::Value & data, BOOL isVerify, const std::string filePath)
{
std::string respBodyData;
std::string respHeadData;
CURL* curl;
CURLcode res;
Logger::HttpInfo(constant, "无请求参数");
struct curl_slist *headers = NULL;
if (isVerify)
{
std::string token;
ReadContantFromFile("token", token, GetCredentialsFilePath);
if (token.empty())
{
MessageBox(NULL, L"当前登录已失效,请重新登录", L"错误", MB_ICONEXCLAMATION);
return S_FALSE;
}
headers = curl_slist_append(headers, ("Authorization:" + token).c_str());
}
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl == NULL)
{
MessageBox(NULL, L"错误的网络连接!", L"错误", MB_ICONERROR);
return S_FALSE;
}
std::string url;
ReadContantFromFile("addr", url, GetCredentialsFilePath);
FILE *file = fopen(filePath.c_str(), "rb");
if (!file) {
MessageBox(NULL, L"文件打开失败,请联系管理员!", L"错误", MB_ICONERROR);
return S_FALSE;
}
size_t pos = filePath.rfind('\\');
std::string fileNameGbk = filePath.substr(pos + 1);
std::string fileNameUtf8;
GbkToUtf8(fileNameGbk, fileNameUtf8);
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "files");
ULONGLONG fileSize = GetFileSize(filePath);
curl_mime_data_cb(part, fileSize, ReadCallback , NULL, NULL, file);
curl_mime_filename(part, fileNameUtf8.c_str());
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, (url + mapping).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WritePostHeaderResp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WritePostBodyResp);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &respHeadData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respBodyData);
bool bCA = FALSE;
if (!bCA)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_easy_setopt(curl, CURLOPT_CAINFO, "");
}
res = curl_easy_perform(curl);
fclose(file);
if (res != CURLE_OK)
{
MessageBox(NULL, CA2W(curl_easy_strerror(res)), L"错误", MB_ICONEXCLAMATION);
return S_FALSE;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
std::string respBodyDataGbk;
Utf8ToGbk(respBodyData, respBodyDataGbk);
std::string result = respBodyDataGbk.c_str();
Logger::HttpInfo(constant, respBodyDataGbk, false);
return ResultHandler(result, data);
}