1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| /** * postForm * url url地址 * f 表单参数 * h header参数 * timeoutMs 超时时间 */ const std::string postForm ( const std::string& url, const FormParams& f, const HeaderParams& h = std::map<std::string, std::string>(), int timeoutMs = 300 ) { //超时时间 curl_easy_setopt(libCurl, CURLOPT_TIMEOUT_MS, timeoutMs); //处理header参数 struct curl_slist* headers = curl_slist_append(NULL, "Expect:");; handlerHeader(h, headers); curl_easy_setopt(libCurl, CURLOPT_HTTPHEADER, headers); const char* charUrl = url.c_str(); curl_mime* form = NULL; curl_mimepart* fieid = NULL; std::string resData; //创建表单 form = curl_mime_init(libCurl); //表单参数 for (auto item : f) { std::string name = std::get<0>(item); std::string value = std::get<1>(item); boolean isFile = std::get<2>(item); //判断是否是文件 if (isFile) { fieid = curl_mime_addpart(form); curl_mime_name(fieid, name.c_str()); curl_mime_filedata(fieid, value.c_str()); }else { fieid = curl_mime_addpart(form); curl_mime_name(fieid, name.c_str()); curl_mime_data(fieid, value.c_str(), CURL_ZERO_TERMINATED); } } curl_easy_setopt(libCurl, CURLOPT_URL, charUrl); curl_easy_setopt(libCurl, CURLOPT_MIMEPOST, form); curl_easy_setopt(libCurl, CURLOPT_WRITEDATA, &resData); curl_easy_setopt(libCurl, CURLOPT_WRITEFUNCTION, CurlSupport::write_callback); CURLcode resCode = curl_easy_perform(libCurl); curl_mime_free(form); if (CURLE_OK != resCode) { throw CurlSupportException(curl_easy_strerror(resCode)); } else{ return resData; } }
|