curl向web服务器发送json数据
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 -;
}
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 != ){
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[] = {};
struct curl_slist* headers = NULL;
if(!url){
return -;
}
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, ); //超时时间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!=){
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 -;
}
} 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();
} 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 ;
}
curl向web服务器发送json数据的更多相关文章
- libcurl HTTP POST请求向服务器发送json数据【转】
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- libcurl HTTP POST请求向服务器发送json数据
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- IOS-网络(发送JSON数据给服务器和多值参数)
三步走: 1.使用POST请求 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Co ...
- ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误
ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件
一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...
- SpringMVC客户端发送json数据时报400错误
当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...
随机推荐
- [原创]抢先DriverStudio夺取机器控制权(上篇)
原文链接:抢先DriverStudio夺取机器控制权 废话不谈,言归正传!大家都知道,装了DriverStudio软件(我使用的是v3.2版)的系统在启动时会显示其配置画面,(如图0所示) 图 0 这 ...
- 使用TWebBrowser组件保存网页为html和mht文件 收藏
一.保存为HTML文件 uses ActiveX;...procedure WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;var ...
- ICON图标文件解析
icon是一种图标格式,用于系统图标.软件图标等,这种图标扩展名为*.icon.*.ico.常见的软件或windows桌面上的那些图标一般都是ICON格式的. ICON文件格式比较简单,包含文件头段. ...
- vim-配置文件
" " Last Change: 2010年08月02日 15时13分 " " Version: 1.80 " "============= ...
- 了解Linux 命名空间
转载: http://laokaddk.blog.51cto.com/368606/674256 命名空间提供了虚拟化的一种轻量级形式,使得我们可以从不同的方面来查看运行系统的全局属性.该机制类似于S ...
- Python-第一天
1. 基础知识 1.1 编码 python默认是ascii编码,不支持中文 在脚本顶部添加 #coding=utf-8,使python支持中文 #!/usr/bin/env python --- ...
- 数组-Find Minimum in Rotated Sorted Array
排序数组在旋转后,可以分为前后两个排序子序列.在没有相同元素的情况下,前一个数组中的元素均大于后一个数组中的元素. 如果我们要找最小元素,则只要找到两个数组的分界点即可,即第二个子序列的开始元素. 由 ...
- ebs清除并法管理器所清除的表
In this Document Goal Solution References Applies to: Oracle Concurrent Processing - Version 1 ...
- Kali linux安装漏洞扫描工具Nessus指南
引子:Nessus是著名信息安全服务公司tenable推出的一款漏洞扫描与分析软件,号称是"世界上最流行的漏洞扫描程序,全世界超过75,000个组织在使用它".虽然这个扫描程序能够 ...
- Javascript知识三
O(∩_∩)O...今天继续学习了Javascript的知识,下面就总结一下. 数组: 判断是否为数组(instanceof Array) 在JavaScript中,数组不一定是同一类型 var ar ...