#include "stdafx.h" #include "CHttpUtil.h" SINGLETON_IMPLEMENT(CHttpUtil) CHttpUtil::CHttpUtil() { // global init curl_global_init(CURL_GLOBAL_ALL); } CHttpUtil::~CHttpUtil() { // global release curl_global_cleanup(); } // reply of the requery size_t CHttpUtil::req_reply(void *ptr, size_t size, size_t nmemb, void *stream) { string *str = (string*)stream; (*str).append((char*)ptr, size*nmemb); return size * nmemb; } // http GET CURLcode CHttpUtil::curl_get_req(const std::string &url, std::string &response) { // init curl CURL *curl = curl_easy_init(); // res code CURLcode res; if (curl) { // set params curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 0); // 是否返回头信息 0:不返回 1:返回 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // start req res = curl_easy_perform(curl); } // release curl curl_easy_cleanup(curl); response = UTF8ToGBK(response); return res; } // http POST CURLcode CHttpUtil::curl_post_req(const string &url, const string &postParams, string &response) { // init curl CURL *curl = curl_easy_init(); // res code CURLcode res; if (curl) { // set params curl_easy_setopt(curl, CURLOPT_POST, 1); // post req curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // start req res = curl_easy_perform(curl); } // release curl curl_easy_cleanup(curl); return res; } string CHttpUtil::UTF8ToGBK(const std::string& strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); WCHAR* wszGBK = new WCHAR[len + 1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL); std::string strTemp(szGBK); delete[]szGBK; delete[]wszGBK; return strTemp; } string CHttpUtil::GBKToUTF8(const std::string& strGBK) { string strOutUTF8 = ""; WCHAR * str1; int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); str1 = new WCHAR[n]; MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n); n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); char * str2 = new char[n]; WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); strOutUTF8 = str2; delete[]str1; str1 = NULL; delete[]str2; str2 = NULL; return strOutUTF8; }