libcurl的使用

来源:互联网 发布:淘宝卖家发短信的软件 编辑:程序博客网 时间:2024/05/20 00:10

本文只是简单介绍了,通过libcurl实现向服务点发送post请求的功能:


代码如下:

CURL *curl;   //创建一个CURL变量

        string steFile = "......";//post请求的内容


curl = curl_easy_init();   //初始化
if(curl) {  
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_WIN32); //初始化版本


// 设置easy handle属性
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.170:10002/image");//连接url
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strFile.size());//获取post内容大小
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strFile.c_str());//全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径
curl_easy_setopt(curl, CURLOPT_POST, 1);//设置非0,表示本次操作为post
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);//设置为非0在执行时打印请求信息
curl_easy_setopt(curl, CURLOPT_HEADER, 1);//设置为非0将响应头信息同响应体一起传给WRITEFUNCTION
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);//设置为非0,响应头信息Location




// 执行数据请求
int ireturn = curl_easy_perform(curl);


// 释放资源
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();

0 0