#include "pch.h" #include "MyNetWorkManager.h" #include // ½â¾ö¼æÈÝÎÊÌâ #if _MSC_VER>=1900 #include "stdio.h" _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned); #ifdef __cplusplus extern "C" #endif FILE * __cdecl __iob_func(unsigned i) { return __acrt_iob_func(i); } #endif /* _MSC_VER>=1900 */ MyNetWorkManager* MyNetWorkManager::__instance = NULL; MyNetWorkManager* MyNetWorkManager::getInstance() { if (NULL == __instance) { __instance = new MyNetWorkManager(); } return __instance; } MyNetWorkManager::MyNetWorkManager() :m_debug(false), SuccessCallBack(nullptr) { } MyNetWorkManager::~MyNetWorkManager() { } void MyNetWorkManager::setSuccesseCallBack(SuccessFinishedCallBack callBack) { SuccessCallBack = callBack; } void MyNetWorkManager::setDebug(bool debug) { m_debug = debug; } size_t MyNetWorkManager::write_data(void* buffer, size_t size, size_t nmenb, void* lpvoid) { string* str = dynamic_cast((string*)lpvoid); if (NULL == str || NULL == buffer) { return -1; } char* pData = (char*)buffer; str->append(pData, size * nmenb); return nmenb; } int MyNetWorkManager::updateProgress(void* ptr, double rDlTotal, double rDlNow, double rUlTotal, double rUlNow) { //CCLOG("½ø¶È%f", rUlNow); return 0; } //getÇëÇó int MyNetWorkManager::GetHttpRequest(const string& strUrl, SuccessFinishedCallBack callback) { string strResponse;//·µ»ØÊý¾Ý CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT);// curl = curl_easy_init();//³õʼ»¯¾ä±ú if (NULL == curl) { return CURLE_FAILED_INIT; } curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());//ÉèÖÃurl curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//µ÷Óô¦Àíº¯Êý curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);//·µ»ØµÄÊý¾Ý£¬ÕâÀï¿ÉÒÔ¼Ó¸öº¯ÊýÖ¸Õë curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);//ÉèÖÃÁ´½Óʱ³¤ res = curl_easy_perform(curl);//¿ªÊ¼ÏÂÔØ if (res == CURLE_OK) { callback(strResponse); } curl_global_cleanup(); curl_easy_cleanup(curl);// return res; } int onDebug(CURL* curl, curl_infotype itype, char* pData, size_t size, void* lpvoid) { if (itype == CURLINFO_TEXT) { printf("[text] %s\n", pData); } else if (itype == CURLINFO_HEADER_IN) { printf("[HEADER_IN] %s\n", pData); } else if (itype == CURLINFO_HEADER_OUT) { printf("[HEADER_IN] %s\n", pData); } else if (itype == CURLINFO_DATA_IN) { printf("[HEADER_IN] %s\n", pData); } else if (itype == CURLINFO_DATA_OUT) { printf("[HEADER_IN] %s\n", pData); } return (int)itype; } //postÇëÇó string MyNetWorkManager::PostHttpRequest(const string& strUrl, const string& filePath, mapparams) { string strResponse;//·µ»ØÊý¾Ý int progress; CURL* curl; CURLcode res; struct curl_httppost* post = NULL; struct curl_httppost* last = NULL; static const char buf[] = "Expect:"; //ÅäÖÃÉÏ´«ÎļþµÄ±íµ¥ if (!filePath.empty()) { curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, filePath.c_str(), CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END); } //ÅäÖòÎÊý for (auto iter = params.begin(); iter != params.end(); iter++) { curl_formadd(&post, &last, CURLFORM_COPYNAME, iter->first.c_str(), CURLFORM_COPYCONTENTS, iter->second.c_str(), CURLFORM_END); } // curl = curl_easy_init();//³õʼ»¯¾ä±ú if (NULL == curl) { throw CURLE_FAILED_INIT; } //if (m_debug) { curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);//Õâ¸öCURLOPT_VERBOSEÉèΪ1£¬½«ÏÔʾ³öËùÓз¢Ë͵ÄʵÌåЭÒéµÄϸ½Ú curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, onDebug);//ÕâÀォµ÷ÓÃonDebugµ÷ÊÔËùÓÐÐÅÏ¢ //} // curl_easy_setopt(curl, CURLOPT_PROXY, "192.168.3.122:8888");// ´úÀí // curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);//±íµ¥ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strResponse); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, TRUE);//ÊÇ·ñÐèÒª½ø¶È //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateProgress);//½ø¶Èº¯Êý //curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress);//½ø¶ÈÊý¾Ý curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 50);//Á¬½Óʱ¼ä res = curl_easy_perform(curl); curl_global_cleanup(); curl_easy_cleanup(curl); curl_formfree(post); if (res == CURLE_OK) { return strResponse; } else { throw res; } }