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();