原文链接:
点击打开链接
一、上传数据
如题:
char name[] = "jgood";
char address[] = "Hangzhou";
上传这两个数据到http://localhost:2210/Default.aspx 该怎么办?
直接上代码:
int main()
{
CURLcode code;
code = curl_global_init(CURL_GLOBAL_ALL ); //初始化libcurl库,想用libcurl库的函数就必须首先初始化libcurl
if(CURLE_OK != return_code)
{
cerr << "init libcurl failed." << endl;
return -1;
}
CURL *easy_handle = curl_easy_init(); //初始化easy interface,想使用easy interface的api函数就必须首先初始化easy interface
/* curl_easy_setopt函数设置属性和操作 */
curl_easy_setopt(easy_handle, CURLOPT_URL, http://localhost:2210/Default.aspx); //这里是设置url就是你想要上传的服务器
curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, "name=jgood&address=hangzhou"); //这里就是想要上传的数据,数据之间用&隔开
curl_easy_perform(easy_handle); //连接到远程主机,发送请求,并接收响应
curl_easy_cleanup(easy_handle); //释放资源
curl_global_cleanup(); //释放资源
return 0;
}
二、上传表单
如题:
char name[] = "jgood";
char address[] = "Hangzhou";
char filepath[] = "/filename.jpg";
上传这两个数据和这个文件到http://localhost:2210/Default.aspx 该怎么办?
直接上代码:
int main()
{
CURLcode code;
code = curl_global_init(CURL_GLOBAL_ALL ); //初始化libcurl库,想用libcurl库的函数就必须首先初始化libcurl
if(CURLE_OK != return_code)
{
cerr << "init libcurl failed." << endl;
return -1;
}
CURL *easy_handle = curl_easy_init(); //初始化easy interface,想使用easy interface的api函数就必须首先初始化easy interface
curl_easy_setopt(easy_handle, CURLOPT_URL, http://localhost:2210/Default.aspx); //设置easy handle的属性和操作
curl_httppost *post = NULL;
curl_httppost *last = NULL;
/* 添加表单,按照服务器给的接口中变量顺序进行添加 */
curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "JGood", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "address", CURLFORM_COPYCONTENTS, "HangZhou", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filepath, CURLFORM_CONTENTTYPE, "application/x-jpg", CURLFORM_END); //application/x-jpg是jpg文件的类型,具体要根据自己上传的文件类型进行设置,参考http://tools.jb51.net/table/http_content_type
curl_easy_perform(easy_handle); //连接到远程主机,发送请求,并接收响应
curl_easy_cleanup(easy_handle); //释放资源
curl_global_cleanup(); //释放资源
return 0;
}
---------------------
作者:jigetage
来源:CSDN
原文:https://blog.csdn.net/jigetage/article/details/80606246
版权声明:本文为博主原创文章,转载请附上博文链接!