libcurl post带参数请求

1、参数特殊字符转换

  curl_easy_escape函数及curl_escape函数都可以将参数中的所有不符合URL规定的字符转化为其对应的%XX 形式。
  curl_escape函数是旧的API,其内部调用的是curl_easy_escape函数只不过handle设置为NULL了。
两个函数调用返回的字符串都要调用curl_free或free函数释放。
在这里插入图片描述

2、设置post参数

  使用CURLOPT_POSTFIELDS添加post参数

  CURL *curl;
  CURLcode res;

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl)
  {
    char* raw_parame = "vhost=__defaltVhost__&app=proxy&stream=1_1&url=rtsp://admin:123456@192.168.2.157:554/cam/realmonitor?channel=1&subtype=0";
  	char* escap_param = curl_escape(raw_parame , strlen(raw_parame ));
  	curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
  	
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, param );
    /* 长参数一定要设置CURLOPT_POSTFIELDSIZE,否则会截断 */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(escap_param ));
    curl_easy_setopt(EasyCurlHandle, CURLOPT_VERBOSE, 1); /* 打开debug */
    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);
	free(escap_param);
    
  }
  curl_global_cleanup();

3、使用CURLOPT_POSTFIELDS

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值