demo使用的是curl-8.3.0.tar.gz,其它版本也可以,安装教程可以去网上搜
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
/* somewhat unix-specific */
#include <sys/time.h>
#include <unistd.h>
/* curl stuff */
#include <curl/curl.h>
#include <curl/mprintf.h>
#define HTTP_DEFAULT_CONN_TIME (30) // 30s,http connect timeout
#define COOKIE_MAX_LEN (100) //http cookie string len max
#define MAX_URL_SIZE (1024) // url最大长度
#define MAX_DATA_SIZE (1024) // url最大长度
#define SFHTTP_OPERATION_TIMEOUT (60) // default operation time out 60s
#define SFHTTP_CONNECT_TIMEOUT (30) // default connect time out 30s
#define SFHTTP_DOWNLOAD_TIMEOUT (600) // 600s
#define SFHTTP_HEADER_EXPECT "Expect:" // continue
#define DOWN_FILE_PATH "./agent.deb"
// #define _DEBUG
#ifdef _DEBUG
static
int
OnDebug(CURL *handle, curl_infotype itype, char *pData, size_t size, void *userp)
{
if (itype == CURLINFO_TEXT) {
printf("[TEXT]%s", pData);
}
if (itype == CURLINFO_HEADER_IN) {
printf("[HEADER_IN]%s", pData);
}
if (itype == CURLINFO_HEADER_OUT) {
printf("[HEADER_OUT]%s", pData);
}
if (itype == CURLINFO_DATA_IN) {
printf("[DATA_IN]%s", pData);
}
if (itype == CURLINFO_DATA_OUT) {
printf("[DATA_OUT]%s", pData);
}
return 0;
}
#endif
static
size_t
OnWriteData(
char * pResponseStream,
size_t size,
size_t nmemb,
void* pContext
)
{
char *pStr = NULL;
assert(pContext != NULL);
pStr = (char *)pContext;
// pStr->append((char*)pResponseStream, size * nmemb);
// snprintf(pStr, size * nmemb, "%s", pResponseStream);
strncpy(pStr, pResponseStream, size * nmemb);
return size * nmemb;
}
static
size_t
OnDownWriteData(char *ptr, size_t uByte, size_t nmemb, void *userdata)
{
FILE *pFile = NULL;
size_t retBytes = 0;
pFile = (FILE*)userdata;
if (userdata == NULL) {
return 0;
}
retBytes = fwrite(ptr, 1, (uByte * nmemb), pFile);
if (retBytes == 0 || retBytes != (uByte * nmemb)) {
return 0;
}
return (size_t)retBytes;
}
void http_get(){
CURL* pCurl = NULL;
int iRet = 0;
long lRespCode = 0;
size_t uConnectime = 0, uOprtime = 0;
char *pszData = NULL;
CURLcode curlCode = CURLE_OK;
char s_uid_cookie[COOKIE_MAX_LEN];
char pszUrl[MAX_URL_SIZE] = {0};
char strData[MAX_DATA_SIZE] = {0};
snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/ping");
pCurl = curl_easy_init();
if (NULL == pCurl) {
fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");
goto L_Cleanup;
}
iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);
// if(NULL != pszUid)
// {
// if(strlen(pszUid) >= 8)
// {
// str_snprintf(s_uid_cookie, COOKIE_MAX_LEN, "company_code=%s", pszUid);
// iRet |= curl_easy_setopt(pCurl, CURLOPT_COOKIE, s_uid_cookie);
// }
// }
uConnectime = SFHTTP_CONNECT_TIMEOUT;
uOprtime = SFHTTP_OPERATION_TIMEOUT;
iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);
iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);
#ifdef _DEBUG
iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteData);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void *)strData);
iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);
if (iRet != 0) {
fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);
goto L_Cleanup;
}
curlCode = curl_easy_perform(pCurl);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
printf("recv Data: %s \n", strData);
L_Cleanup:
if (pCurl) {
curl_easy_cleanup(pCurl);
pCurl = NULL;
}
}
void http_post(){
CURL* pCurl = NULL;
int iRet = 0;
long lRespCode = 0;
size_t uConnectime = 0, uOprtime = 0;
char *pszData = NULL;
CURLcode curlCode = CURLE_OK;
struct curl_slist * pHeader = NULL;
char s_uid_cookie[COOKIE_MAX_LEN];
char pszUrl[MAX_URL_SIZE] = {0};
char strData[MAX_DATA_SIZE] = {0};
snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/submit");
pCurl = curl_easy_init();
if (NULL == pCurl) {
fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");
goto L_Cleanup;
}
pHeader = curl_slist_append(pHeader, "Content-Type: application/json");
if (pHeader == NULL) {
fprintf(stderr, "curl_easy_init or curl_slist_append failed!\n");
goto L_Cleanup;
}
iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);
uConnectime = SFHTTP_CONNECT_TIMEOUT;
uOprtime = SFHTTP_OPERATION_TIMEOUT;
iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);
iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);
#ifdef _DEBUG
iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
// iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);
// iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);
iRet |= curl_easy_setopt(pCurl, CURLOPT_POST, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteData);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void *)strData);
iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);
// iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "name=hds&email=curl");
iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "{\"name\":\"hds\",\"email\":\"curl@qq.com\"}");
iRet |= curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pHeader);
if (iRet != 0) {
fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);
goto L_Cleanup;
}
curlCode = curl_easy_perform(pCurl);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
printf("recv Data: %s \n", strData);
L_Cleanup:
if (pCurl) {
curl_easy_cleanup(pCurl);
pCurl = NULL;
}
}
void http_down_file(){
CURL* pCurl = NULL;
int iRet = 0;
long lRespCode = 0;
size_t uConnectime = 0, uOprtime = 0;
char *pszData = NULL;
CURLcode curlCode = CURLE_OK;
struct curl_slist * pHeader = NULL;
char s_uid_cookie[COOKIE_MAX_LEN];
char pszUrl[MAX_URL_SIZE] = {0};
// char strData[MAX_DATA_SIZE] = {0};
char pszFilePath[MAX_URL_SIZE] = {0};
/* 创建文件 */
FILE* pFile = NULL;
strncpy(pszFilePath, DOWN_FILE_PATH, strlen(DOWN_FILE_PATH));
pFile = fopen(pszFilePath, "wb");
if (pFile == NULL) {
fprintf(stderr,"fopen %s failed with error code = %d", pszFilePath, errno);
goto L_Cleanup;
}
snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/down_file");
pCurl = curl_easy_init();
if (NULL == pCurl) {
fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");
goto L_Cleanup;
}
pHeader = curl_slist_append(pHeader, "Content-Type: application/json");
if (pHeader == NULL) {
fprintf(stderr, "curl_easy_init or curl_slist_append failed!\n");
goto L_Cleanup;
}
iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);
uConnectime = SFHTTP_CONNECT_TIMEOUT;
uOprtime = SFHTTP_OPERATION_TIMEOUT;
iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);
iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);
#ifdef _DEBUG
iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif
iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);
// iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);
iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);
iRet |= curl_easy_setopt(pCurl, CURLOPT_POST, 1);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnDownWriteData);
iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pFile);
iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);
iRet |= curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_LIMIT , 1); // 最低下载速度 1byte/sec,超过10min断开链接
iRet |= curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_TIME , SFHTTP_DOWNLOAD_TIMEOUT);
// iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "name=hds&email=curl");
iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "{\"file_name\":\"agent.deb\"}");
iRet |= curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pHeader);
if (iRet != 0) {
fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);
goto L_Cleanup;
}
curlCode = curl_easy_perform(pCurl);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);
if (curlCode != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));
goto L_Cleanup;
}
// printf("recv Data: %s \n", strData);
L_Cleanup:
if(pFile){
fclose(pFile);
}
if (pCurl) {
curl_easy_cleanup(pCurl);
pCurl = NULL;
}
}
// 编译:gcc -o curl_get curl_get.c -lcurl -I/root/code/dynamic_so/curl/include -L/root/code/dynamic_so/curl/lib
int main(){
printf("hello world \n");
http_down_file();
return 0;
}