概述
由于Get方式可以直接在浏览器显示的,但是post的json方式不能直接在浏览器,所以这里讲解一下curl的post的json方法
//使用curl库,以post方式向服务器发送json数据
//json数据的组合可以参考jsoncpp库,也可以按json格式自己组合字符串
//注意事项,以下代码不可以多线程执行,如果多线程执行,需要加锁进行控制,否则会运行崩溃
JSON格式post方法
[cpp]
view plain
copy
- #include <curl/curl.h>
- #include <string>
- #include <exception>
- int main(int argc, char *argv[])
- {
- char szJsonData[1024];
- memset(szJsonData, 0, sizeof(szJsonData));
- std::string strJson = "{";
- strJson += ""user_name" : "test",";
- strJson += ""password" : "test123"";
- strJson += "}";
- strcpy(szJsonData, strJson.c_str());
- try
- {
- CURL *pCurl = NULL;
- CURLcode res;
- // In windows, this will init the winsock stuff
- curl_global_init(CURL_GLOBAL_ALL);
- // get a curl handle
- pCurl = curl_easy_init();
- if (NULL != pCurl)
- {
- // 设置超时时间为1秒
- curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);
- // First set the URL that is about to receive our POST.
- // This URL can just as well be a
- // https:// URL if that is what should receive the data.
- curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.svc");
- //curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.cgi");
- // 设置http发送的内容类型为JSON
- curl_slist *plist = curl_slist_append(NULL,
- "Content-Type:application/json;charset=UTF-8");
- curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
- // 设置要POST的JSON数据
- curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);
- // Perform the request, res will get the return code
- res = curl_easy_perform(pCurl);
- // Check for errors
- if (res != CURLE_OK)
- {
- printf("curl_easy_perform() failed:%sn", curl_easy_strerror(res));
- }
- // always cleanup
- curl_easy_cleanup(pCurl);
- }
- curl_global_cleanup();
- }
- catch (std::exception &ex)
- {
- printf("curl exception %s.n", ex.what());
- }
- return 0;
- }
参考文档curl压缩包docsexampleshttp-post.c
curl压缩包下载地址:http://curl.haxx.se/download.html
以下是c代码的实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define POSTURL "http://www.xiami.com/member/login"
#define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
#define FILENAME "curlposttest.log"
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
FILE *fptr = (FILE*)userp;
fwrite(buffer, size, nmemb, fptr);
return size * nmemb;//注意这里必须要这样写不然会出错
}
curl de POST 方法
uint64_t curl_post_content (const char * url, char * content)//返回下载数据长度
{
CURL *curl;
CURLcode res;
FILE *fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME, "w")) == NULL) {
fprintf(stderr, "fopen file error: %sn", FILENAME);
exit(1);
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"json={"total":50.5,"userId":12345,"productId":1,"productPrice":10.1,"equipmentId":1,"productCount":5,"productRoughness":2}"); //设置post属性,使用&来将表单属性连接在一起
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //回调函数,可有可无
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); //回调函数写入数据指针
curl_easy_setopt(curl, CURLOPT_POST, 1); //设置libcurl发送的协议
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //设置版本
curl_easy_setopt(curl, CURLOPT_HEADER, 1); //设置http数据头
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置返回的数据量
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "curlposttest.cookie"); //设置cookie,不是必须
res = curl_easy_perform(curl);
printf("--->>>return is %dn",res);
curl_easy_cleanup(curl);
}
FILE *fptr = (FILE*)userp;
fwrite(buffer, size, nmemb, fptr);
return size * nmemb;//注意这里必须要这样写不然会出错
}
curl de POST 方法
uint64_t curl_post_content (const char * url, char * content)//返回下载数据长度
{
CURL *curl;
CURLcode res;
FILE *fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME, "w")) == NULL) {
fprintf(stderr, "fopen file error: %sn", FILENAME);
exit(1);
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"json={"total":50.5,"userId":12345,"productId":1,"productPrice":10.1,"equipmentId":1,"productCount":5,"productRoughness":2}"); //设置post属性,使用&来将表单属性连接在一起
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //回调函数,可有可无
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); //回调函数写入数据指针
curl_easy_setopt(curl, CURLOPT_POST, 1); //设置libcurl发送的协议
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //设置版本
curl_easy_setopt(curl, CURLOPT_HEADER, 1); //设置http数据头
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置返回的数据量
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "curlposttest.cookie"); //设置cookie,不是必须
res = curl_easy_perform(curl);
printf("--->>>return is %dn",res);
curl_easy_cleanup(curl);
}
欢迎关注并加入物联网行业联盟,积累行业人脉和资源。
最后
以上就是欢呼悟空为你收集整理的使用curl库,以post方式向服务器发送json/字符串数据的全部内容,希望文章能够帮你解决使用curl库,以post方式向服务器发送json/字符串数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复