照着教程编译操作都没问题
首先是配置curl库
给一个别人的编译链接curl库 vs2017:亲测可用
c++编译curl库
测试代码:
#include <iostream>
using namespace std;
int main()
{
curl_easy_init();
return 0;
}
没报错即配置成功
下面是上传json数据代码
(下面以字符串为例子)
我手动拼接json字符串就不用配置json库了
(配置json库在下面)
#include <curl/curl.h>
#include <string>
#include <exception>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char szJsonData[1024];
memset(szJsonData, 0, sizeof(szJsonData));
string aa = "123";
string strJson = "{";
strJson += "\"hex\" : \"" + aa + "\",";
strJson += "\"aa\" : \"123\",";
strJson += "\"bb\" : \"123\",";
strJson += "\"cc\" : \"123\"";
strJson += "}";
strcpy(szJsonData, strJson.c_str());
try
{
CURL *pCurl = NULL;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
pCurl = curl_easy_init();
if (NULL != pCurl)
{
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(pCurl, CURLOPT_URL, "your URL");
curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);
res = curl_easy_perform(pCurl);
if (res != CURLE_OK)
{
printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(pCurl);
}
curl_global_cleanup();
}
catch (std::exception &ex)
{
printf("curl exception %s.\n", ex.what());
}
return 0;
}
配置json库:
有两个版本,一个支持c++11 是1.几版本的,一个不支持0.10.7(我用的是这个)
同样也是找别人的:亲测可用
c++配置json库
下面是测试代码
#include <iostream>
#include "json/json.h"
using namespace std;
int main()
{
string aa = "123";
Json::Value jsonItem;
jsonItem["hexImage"] =aa;
cout << jsonItem.toStyledString() << endl;
string str=jsonItem.toStyledString();
Json::Reader reader;
Json::Value root;
if (reader.parse(str, root))
{
string jsstr= root["hexImage"].asString();
cout << "jsstr:" << jsstr<< endl;
}
return 0;
}
下面给一个封装curl的类给大家使用。需要根据你需要的版本进行编译对应x86和x64
包含库路径:
E:\C++库\curl库\cur\libcurl-vc14-x86-release-static-ipv6-sspi-winssl\include
预处理器:
CURL_STATICLIB
_CRT_SECURE_NO_WARNINGS
包含库路径和依赖库名字:
把lib拷到对应的文件下
依赖库:
libcurl_a.lib
ws2_32.lib
winmm.lib
wldap32.lib
Crypt32.lib
Normaliz.lib
头文件
#pragma once
#include <iostream>
using namespace std;
class Httpcurl
{
public:
void initcurl();
void closecurl();
std::string requesthttp(std::string sendstr, std::string url);
};
cpp文件
#include "Httpcurl.h"
#include <curl/curl.h>
size_t http_data_writer(void* data, size_t size, size_t nmemb, void* content)
{
long totalSize = size * nmemb;
std::string* symbolBuffer = (std::string*)content;
if (symbolBuffer)
{
symbolBuffer->append((char *)data, ((char*)data) + totalSize);
}
return totalSize;
}
void Httpcurl::initcurl()
{
curl_global_init(CURL_GLOBAL_ALL);
}
void Httpcurl::closecurl()
{
curl_global_cleanup();
}
std::string Httpcurl::requesthttp(std::string sendstr, std::string url)
{
std::string strData;
CURL *pCurl = NULL;
CURLcode res;
pCurl = curl_easy_init();
if (NULL != pCurl)
{
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(pCurl, CURLOPT_URL, const_cast<char *>(url.c_str()));
curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, sendstr.c_str());
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, http_data_writer);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void*)&strData);
res = curl_easy_perform(pCurl);
if (res != CURLE_OK)
{
printf("请求失败\n");
return "error";
}
else
{
cout << "请求成功" << endl;
}
curl_easy_cleanup(pCurl);
}
return strData;
}
main.cpp
#include <iostream>
#include "json/json.h"
#include "Httpcurl.h"
using namespace std;
int main(int argc, char *argv[])
{
Json::Value jsonItem;
jsonItem["aaa"] = "123";
jsonItem["bbb"] = "456";
string str = jsonItem.toStyledString();
string url = "http://localhost:8080/test";
Httpcurl httpcurl;
httpcurl.initcurl();
cout << httpcurl.requesthttp(str,url) << endl;
httpcurl.closecurl();
getchar();
return 0;
}
响应什么大家自己用web写个接口测试吧。我这里是获取了它返回的数据。
也可以根据返回的状态码进行判断,是否接收成功。根据实际要求操作。
下面给一个官方的curlAPI地址:
官方API地址
参考文章:
网友幸福官请求响应
curl使用教程
更新2022-02-09
解析json
string getValue(Json::Value jsonItem,string key)
{
if (jsonItem.isMember(key))
{
return jsonItem[key].asString();
}
return "error";
}
Json::Value getRootValue(string str, string key)
{
Json::Reader reader;
Json::Value root;
Json::Value value;
if (reader.parse(str, root))
{
int size = root.size();
for (int j = 0; j < size; j++)
{
if (!root[key].isNull())
{
return root[key];
}
}
}
return value;
}
string str;
str = jsonItem.toStyledString();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)