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数据的更多相关文章

  1. libcurl HTTP POST请求向服务器发送json数据【转】

    转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...

  2. libcurl HTTP POST请求向服务器发送json数据

    转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...

  3. iOS开发网络篇—发送json数据给服务器以及多值参数

    iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...

  4. 【转】iOS开发网络篇—发送json数据给服务器以及多值参数

    原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...

  5. IOS-网络(发送JSON数据给服务器和多值参数)

    三步走: 1.使用POST请求 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Co ...

  6. 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 ...

  7. python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)

    昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...

  8. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

  9. SpringMVC客户端发送json数据时报400错误

    当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...

随机推荐

  1. MSI文件静默安装

    以.net4为例,以下命令为静默安装: dotNetFx40_Full_x86_x64.exe /q /norestart /ChainingPackage FullX64Bootstrapper / ...

  2. opengl笔记

    GL_ARRAY_BUFFER(表示顶点数据) GL_ELEMENT_ARRAY_BUFFER(表示索引数据) GL_PIXEL_UNPACK_BUFEER( 表示传递给O p e n G L 的像素 ...

  3. Table的分割线偏移量设置 及其 UIEdgeInset详解

    -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...

  4. 14.java.lang.EOFException

    java.lang.EOFException 文件已结束异常 当程序在输入的过程中遇到文件或流的结尾时,引发异常.因此该异常用于检查是否达到文件或流的结尾

  5. 使用Ramdisk 加速 Visualstudio 编译调试

    一般来说ASP.NET在执行的时候,会先动态编译在目录 C:\Windows\Microsoft.NET\Framework64\版本\Temporary ASP.NET Files 由于每次修改程序 ...

  6. lua IDE for cocos2d-x development

    原文链接:http://hi.baidu.com/balduc0m/item/648093dad238bd2a39f6f78e lua IDE for cocos2d-x development -- ...

  7. at org.apache.jsp.index_jsp._jspInit(index_jsp.java:23)异常解决

    部署项目,启动tomcat一切正常.输入项目地址后 tomcat报例如以下错误: java.lang.NullPointerException at org.apache.jsp.index_jsp. ...

  8. hdu1114Piggy-Bank(DP完全背包)

    题意:在ACM可以做任何事情,必须准备和预算获得必要的财政支持.这次行动的主要收入来自不可逆绑定金钱(IBM).背后的想法很简单.每当一些ACM成员有任何小的钱,他把所有的硬币和成小猪银行抛出.你知道 ...

  9. Objective-C内存管理教程和原理剖析(三)

    初学Objective-C的朋友都有一个困惑,总觉得对Objective-C的内存管理机制琢磨不透,程 序经常内存泄漏或莫名其妙的崩溃.我在这里总结了自己对Objective-C内存管理机制的研究成果 ...

  10. SQL Server索引进阶第十一篇:索引碎片分析与解决

    相关有关索引碎片的问题,大家应该是听过不少,也许也很多的朋友已经做了与之相关的工作.那我们今天就来看看这个问题. 为了更好的说明这个问题,我们首先来普及一些背景知识. 知识普及 我们都知道,数据库中的 ...