libcurl提供类似curl命令的网络访问api,jsoncpp用来解析json
libcurl安装需要https://curl.haxx.se/download.html 下载相关包
或者
sudo yum install libcurl-devel
sudo apt install libcurl4-openssl-dev sudo apt install libcurl4-nss-dev sudo apt install libcurl4-gnutls-dev
yum和apt命令可以安装https://www.jianshu.com/p/bd1552a29aa6
或者源码编译 http://sourceforge.net/projects/jsoncpp/files/
libcurl除linux的必要依赖之外,还依赖libssl.so和 libcrypto.so
jsoncpp只依赖系统必要依赖
jsoncpp入口头文件是json.h,但是包含的使用需要#include
missing binary operator before token “(”
因此 include目录设置到 jsoncpp目录,下一层是json
curl 引入用
#include
string url = "https://xxx";
string data = "yyyyyyyy";
cout << "input:" << data << endl;
stringstream out;
//请求头
struct curl_slist *headers=NULL; // init to NULL is important
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "charsets: utf-8");
headers = curl_slist_append(headers, "User-Agent: test");
void * curl = curl_easy_init();
//设置url
curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
//输入
//urlecode
string content_url = curl_easy_escape(curl,content.c_str(),0);
data += content_url;
struct curl_slist *pCurlList = NULL;
pCurlList = curl_slist_append(pCurlList, "Content-Type: application/x-www-form-urlencoded");//指定文本url编码
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pCurlList);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_USERAGENT,"test");
//设置接收数据的处理函数和变量
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&out);
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK){
cout << "excute curl failed!" << endl;
}
//获取结果,返回json串
string json_str = out.str();
curl_easy_cleanup(curl);
Json::Reader reader;
Json::Value value;
if(reader.parse(json_str,value)){
// cout << value.toStyledString() <
string token = getEncryptedContent(fieldA + "&" + timestamp);
stringstream out;
//请求头
struct curl_slist *headers=NULL; // init to NULL is important
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
void * curl = curl_easy_init();
//设置url
curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
//输入
Json::Value root;
Json::FastWriter writer;
Json::Value input_json;
input_json["a"] = "a";
input_json["b"] = b;
input_json["c"] = c;
input_json["d"] = "d";
input_json["e"] = "e";
input_json["f"] = "f-";
string input_str = writer.write(input_json);
cout << "request:" << input_str << endl;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, input_str.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
//设置请求头
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
//设置接收数据的处理函数和变量
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&out);
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK){
cout << "excute curl failed!" << endl;
}
//获取结果,返回json串
string json_str = out.str();
curl_easy_cleanup(curl);
Json::Reader reader;
Json::Value value;
if(reader.parse(json_str,value)){
cout << value.toStyledString() <