linux curl编译,Linux openssl curl编译

#include size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)

{

FILE *fptr = (FILE*)stream;

fwrite(buffer,size,nmemb,fptr);

//printf("buf:%s\n", buffer);

return size*nmemb;

}

char *pszBuf = "{\"message_index\":123456,\"gateway_id\":\"00001234\",\"gateway_ip\":\"192.168.1.199\",\"gateway_mac\":\"11:22:33:44:55:66\",\"device_number\":\"2\",\"camera_devices\":[{\"device_ip\":\"192.168.1.200\",\"device_type\":\"IPC-HFS7841-Z-LED-JG\",\"device_mac\":\"11:22:33:44:55:66\",\"device_stat\":\"online\",\"device_SerialNo\":\"123456789012\",\"device_SoftVersion\":\"1.1.1\",\"device_PassWord\":\"online\",\"device_UserName\":\"online\"},{\"device_ip\":\"192.168.1.200\",\"device_type\":\"IPC-HFS7841-Z-LED-JG\",\"device_mac\":\"11:22:33:44:55:66\",\"device_SerialNo\":\"123456789012\",\"device_SoftVersion\":\"1.1.1\",\"device_PassWord\":\"online\",\"device_UserName\":\"online\"}]}";

static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)

{

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_OUT]%s\n", pData);

}

else if(itype == CURLINFO_DATA_IN)

{

printf("[DATA_IN]%s\n", pData);

}

else if(itype == CURLINFO_DATA_OUT)

{

printf("[DATA_OUT]%s\n", pData);

}

return 0;

}

#if 1

int main()

{

CURLcode res;

CURL* curl = curl_easy_init();

if (NULL == curl)

{

printf("Init failed;\n");

return CURLE_FAILED_INIT;

}

printf("Init success\n");

if ( true )//用于调试的设置

{

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);

}

curl_easy_setopt(curl, CURLOPT_URL, "");

curl_easy_setopt(curl, CURLOPT_POST, 1);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, pszBuf);

curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);

//curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);

curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 3000);

curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);  //支持服务器跳转

curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);  // enable TCP keep-alive for this transfer

curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);     // keep-alive idle time to 120 seconds

curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);     // interval time between keep-alive probes: 60 seconds

struct curl_slist* headers = NULL;

headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(curl, CURLOPT_POST, 1);//设置为非0表示本次操作为POST

printf("Start curl\n");

res = curl_easy_perform(curl);

printf("end curl\n");

curl_easy_cleanup(curl);

curl_slist_free_all (headers);

return res;

//retur 0;

}

#else

int main(int argc, char *argv[])

{

CURL *curl;

CURLcode res;

char szBuf[1024 * 10 + 1];

struct curl_httppost *formpost=NULL;

struct curl_httppost *lastptr=NULL;

struct curl_slist *headerlist=NULL;

static const char buf[] = "Expect:";

curl_global_init(CURL_GLOBAL_ALL);

/* Fill in the file upload field */

#if 1

curl_formadd(&formpost,

&lastptr,

CURLFORM_COPYNAME, "file",

CURLFORM_FILE, "./1234.png",

CURLFORM_END);

#else

curl_formadd(&formpost, &lastptr,  CURLFORM_PTRNAME, "file", CURLFORM_FILE, "./11.png", CURLFORM_PTRCONTENTS, szBuf, CURLFORM_CONTENTSLENGTH, 1024 * 10, CURLFORM_END);

#endif

/* Fill in the filename field */

/*curl_formadd(&formpost,

&lastptr,

CURLFORM_COPYNAME, "filename",

CURLFORM_COPYCONTENTS, "sign.txt",

CURLFORM_END);

*/

/* Fill in the submit field too, even if this is rarely needed */

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "gateway_id", CURLFORM_COPYCONTENTS, "00000123", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "space_num", CURLFORM_COPYCONTENTS, "001", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "license", CURLFORM_COPYCONTENTS, "京A12345", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "plate_type", CURLFORM_COPYCONTENTS, "Normal", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "plate_color", CURLFORM_COPYCONTENTS, "Blue", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "date", CURLFORM_COPYCONTENTS, "2020-11-20 18:00:00", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "device_ip", CURLFORM_COPYCONTENTS, "192.168.1.1", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "device_type", CURLFORM_COPYCONTENTS, "IPC", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "device_mac", CURLFORM_COPYCONTENTS, "11:11:22:22:33:44", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "message_index", CURLFORM_COPYCONTENTS, "1", CURLFORM_END);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "protocol_version", CURLFORM_COPYCONTENTS, "1.0.0", CURLFORM_END);

curl_formadd(&formpost,

&lastptr,

CURLFORM_COPYNAME, "submit",

CURLFORM_COPYCONTENTS, "Submit",

CURLFORM_END);

curl = curl_easy_init();

/* initalize custom header list (stating that Expect: 100-continue is not

wanted */

headerlist = curl_slist_append(headerlist, buf);

if(curl) {

/* what URL that receives this POST */

curl_easy_setopt(curl, CURLOPT_URL, "");

if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )

/* only disable 100-continue header if explicitly requested */

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);

/* Perform the request, res will get the return code */

res = curl_easy_perform(curl);

/* Check for errors */

if(res != CURLE_OK)

fprintf(stderr, "curl_easy_perform() failed: %s\n",

curl_easy_strerror(res));

/* always cleanup */

curl_easy_cleanup(curl);

/* then cleanup the formpost chain */

curl_formfree(formpost);

/* free slist */

curl_slist_free_all (headerlist);

}

return 0;

}

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值