curl向web服务器发送json数据
2015-10-23 11:09
513 查看
c++使用libcurl:
/* *g++ demo.cpp -g -Wall -lcurl */ #include <string.h> #include <stdlib.h> #include <stdio.h> #include <iostream> #include <curl/curl.h> int getUrl(const char* filename) { CURL* curl; CURLcode res; FILE* fp; if((fp=fopen(filename, "w"))==NULL){ return -1; } struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Accept:Agent-007"); curl = curl_easy_init(); if(curl){ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com"); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //0: success, other: failure res = curl_easy_perform(curl); if(res != 0){ std::cout <<"error:"<<curl_easy_strerror(res) << std::endl; } curl_slist_free_all(headers); curl_easy_cleanup(curl); fclose(fp); } return res; } /* int postUrl(const char* filename) { CURL* curl; CURLcode res; FILE* fp; if((fp=fopen(filename, "w"))==NULL){ return 1; } curl = curl_easy_init(); if(curl){ } } */ //typedef int (* func)(int, int); typedef size_t (*cb_func)(void*, size_t, size_t, void*); 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; } int PostData(const char* postdata, const char* url, cb_func write_data, void* fptr) //int PostData(const char* postdata, const char* url) { CURL* curl = NULL; CURLcode res; char tmp[32] = {0}; struct curl_slist* headers = NULL; if(!url){ return -1; } std::cout <<"send data url:" << url << std::endl; snprintf(tmp, sizeof(tmp), "Content-Length: %d", (int)strlen(postdata)); std::cout <<"Content-Length: " << tmp << std::endl; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "charset: utf-8"); headers = curl_slist_append(headers, tmp); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl){ curl_easy_setopt(curl, CURLOPT_URL,url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1); //超时时间1s curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //debug res = curl_easy_perform(curl); if(res!=0){ std::cout<<"error no:"<<curl_easy_strerror(res)<<std::endl; } curl_slist_free_all(headers); curl_easy_cleanup(curl); return res; }else{ curl_slist_free_all(headers); return -2; } } int main() { const char* tt = "./aa.out"; int aa = getUrl(tt); std::cout << "return :" << aa << std::endl; FILE* fptr; if((fptr=fopen("./result", "w"))==NULL){ std::cout << "file open error" << std::endl; exit(1); } const char* jsondata = "{\"usrname\": \"cp\", \"passwd\": \"test\"}"; const char* url = "http://192.168.2.163:8080/mystorage/mytest.php"; aa = PostData(jsondata, url, write_data, fptr); return 0; }
相关文章推荐
- 深入浅析javascript立即执行函数
- 指定区域的图片自动按比例缩小的js代码(防止页面被图片撑破)
- 用js保存页面指定内容
- jsf 实例代码
- 仿淘宝分页按钮效果简单美观易使用的JS分页控件
- jsf web.xml配置解析
- js中的escape()、encodeURI()和encodeURIComponent()
- JS escape、encodeURI 、encodeURIComponent 编码与解码
- 深入理解JavaScript定时机制
- js判断手机号运营商的方法
- JS字符串大小写转换实现方式
- js计算文本框输入的字符数
- javascript中prototype的理解
- 正确使用JS中的正则
- 日常收集JS邮箱验证正则表达式
- js封装id与class选择器代码,兼容ie低版本
- Asm.js
- JS实现超简洁网页title标题跑动闪烁提示效果代码
- (三)seajs中的模块标识
- 关于js动态生成元素,注册(绑定)的事件失效的解决(on)