您的位置:首页 > Web前端 > JavaScript

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: