libcurl使用

static build need define macro CURL_STATICLIB in use project

libcurl.lib ws2_32.lib winmm.lib wldap32.lib

struct MemoryStruct {
char *memory;
size_t size;
};

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
OutputDebugString(_T("yGPC:not enough memory (realloc returned NULL)\n"));
return 0;
}

memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

/**
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 30 Sep 2016 07:34:51 GMT

{"head":{"errorCode":"000000","errorMsg":"success"},"body":{"commands":null}

*/
int CMainDlg::httpPost(const std::string & strUrl, const std::string & strPostField, std::string & strRet)
{

std::wstring str = _T("yGPC: CMainDlg::httpPost strUrl:");
str += m_ut.s2w(strUrl) + _T(" postField: ");
str += m_ut.s2w(strPostField);
m_ut.strRemoveByParam(str, _T("\r\n"));
OutputDebugString(str.c_str());

CURL *curl;
CURLcode res;
struct curl_slist *http_header = NULL;
struct MemoryStruct chunk;
chunk.memory = (char*)malloc(1); /* will be grown as needed by realloc above */
chunk.size = 0; /* no data at this point */

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl)
{
OutputDebugString(_T("yGPC:curl init failed\n"));
return -1;
}

std::string strUrlEncode = boost::locale::conv::to_utf<char>(strUrl,"GBK");
curl_easy_setopt(curl,CURLOPT_URL,strUrlEncode.c_str()); //url地址
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,strPostField.c_str()); //post参数
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteMemoryCallback); //对返回的数据进行操作的函数地址
curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk); //这是write_data的第四个参数值
curl_easy_setopt(curl,CURLOPT_POST,1); //设置问非表示本次操作为post
curl_easy_setopt(curl,CURLOPT_VERBOSE,1); //打印调试信息
// curl_easy_setopt(curl,CURLOPT_HEADER,1); //将响应头信息和相应体一起传给write_data
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //设置为非,响应头信息location
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"/curlposttest.cookie");
curl_easy_setopt(curl,CURLOPT_TIMEOUT,10);


res = curl_easy_perform(curl);

if (res != CURLE_OK)
{
switch(res)
{
case CURLE_UNSUPPORTED_PROTOCOL:
OutputDebugString(_T("yGPC:CURLE_UNSUPPORTED_PROTOCOL\n"));
break;
case CURLE_COULDNT_CONNECT:
OutputDebugString(_T("yGPC:CURLE_COULDNT_CONNECT\n"));
break;
case CURLE_HTTP_RETURNED_ERROR:
OutputDebugString(_T("yGPC:CURLE_HTTP_RETURNED_ERROR\n"));
break;
case CURLE_READ_ERROR:
OutputDebugString(_T("yGPC:CURLE_READ_ERROR\n"));
break;
default:
{
std::string str = ("yGPC:");
str += ("return code:");
str += m_ut.w2s(m_ut.number2str(res,_T("%x")));
OutputDebugString(_T("yGPC:CURLE_READ_ERROR\n"));
}
break;
}
return -1;
} else
{
strRet = chunk.memory;
}
curl_easy_cleanup(curl);
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();

return 0;

}

jo["signature"] = genSignature(strTimestamp,strRandom).c_str();
jo["timestamp"] = strTimestamp.c_str();
jo["requestTime"] = strTimestamp.c_str();
jo["nonce"] = strRandom.c_str();
jsonRoot["head"] = jo;

std::string param = "msg=";
param += jsonRoot.toStyledString();
std::string strUrlEncode = boost::locale::conv::to_utf<char>(param,"GBK");
std::string strRet;
pwnd->httpPost(url,strUrlEncode,strRet);