int cloud_https_post(const char *pUrl,
const char *request,
char **response,
st_http_resinfo *pHttpResInfo)
{
CURLcode res;
CURL* curl = NULL;
struct curl_slist *headers = NULL;
#ifdef CLOUD_HTTPS_DEBUG
char errbuf[CURL_ERROR_SIZE];
memset(errbuf, '\0', CURL_ERROR_SIZE);
#endif
if (NULL == pUrl || NULL == request || NULL == response || NULL == pHttpResInfo)
{
return CURLE_FAILED_INIT;
}
res = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl global init fail and ret %d", res);
return res;
}
curl = curl_easy_init();
if (NULL == curl)
{
res = CURLE_FAILED_INIT;
HTTPS_LOG(LOG_LEVEL_ERROR, "curl init fail");
goto exit;
}
headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8");
if (NULL == headers)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl get header list fail");
goto exit;
}
#ifdef CLOUD_HTTPS_DEBUG
//provide a buffer to store errors in
res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_ERRORBUFFER ret %d", res);
goto exit;
}
#endif
if (IS_SESSION_DEBUG_ON())
{
HTTPS_LOG(LOG_LEVEL_DEBUG, "post sesstion debug on");
res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_VERBOSE ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_DEBUGFUNCTION ret %d", res);
goto exit;
}
}
res = curl_easy_setopt(curl, CURLOPT_URL, pUrl);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_URL ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_HTTPHEADER ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_POST, 1);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POST ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POSTFIELDS ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_READFUNCTION ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData_Post);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEFUNCTION ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEDATA ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_NOSIGNAL ret %d", res);
goto exit;
}
if (IS_CA_PATH_NULL())
{
res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYHOST ret %d", res);
goto exit;
}
}
else
{
res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_CAINFO, GET_CA_PATH());
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CAINFO ret %d", res);
goto exit;
}
if (GET_CA_TYPE())
{
res = curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,GET_CA_TYPE());
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSLCERTTYPE ret %d", res);
goto exit;
}
}
else
{
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
}
}
res = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, GET_P_CONNECT_TIMEOUT());
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CONNECTTIMEOUT ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, GET_P_TRANSFER_TIMEOUT());
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_TIMEOUT ret %d", res);
goto exit;
}
res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
if (CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_FOLLOWLOCATION ret %d", res);
goto exit;
}
res = curl_easy_perform(curl);
#ifdef CLOUD_HTTPS_DEBUG
if(CURLE_OK != res)
{
HTTPS_LOG(LOG_LEVEL_DEBUG, "curl post return error: %s", errbuf);
}
#endif
curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &(pHttpResInfo->status_code));
HTTPS_LOG(LOG_LEVEL_DEBUG, "cloud_https_post done. ret %d, http status code %ld",
res, pHttpResInfo->status_code);
exit:
if (headers)
{
curl_slist_free_all(headers);
}
if (curl)
{
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return res;
}解析函数
最新发布