Fork me on GitHub
0%

Linux环境下C++访问web服务——使用libcurl库调用http接口发送解析json数据

一、背景

 这两天由于一些原因研究了研究如何在客户端C++代码中调用web服务端接口,需要访问url,并传入json数据,拿到返回值,并解析。
 现在的情形是远程服务端的接口参数和返回类型都是json的字符串。所以我们主要做的就是:连接远程url、找到接口,将基本类型的数据封装成json数据传入接口,然后获取返回值,并解析返回的json数据。
 这里需要用到的库就有url库、json库,大致了解了,接下来就要下载包进行配置了。

二、配置环境

首先需要安装curl库和json库,安装详情如下:

  • 安装curl库

    1.下载:wget http://curl.haxx.se/download/curl-7.38.0.tar.gz (如果下载不了,直接在浏览器打开这个网址下载下来)
    2.解压:tar -xzvf curl-7.38.0.tar.gz
    3.安装:

    1
    2
    3
    4
    cd curl-7.38.0
    ./configure
    sudo make
    sudo make install

4.查看/usr/include目录下有没有curl文件夹,没有的话需要将解压包/curl-7.38.0/include中的curl拷贝过去
5.查看/usr/local/lib/目录下有没有libcurl.so.4.3.0和libcurl.so,没有的话将/curl-7.38.0/lib/.libs/libcurl.so.4.3.0拷贝到/usr/local/lib/下,并建立软链接:ln -s libcurl.so.4.3.0 libcurl.so
6.将路径加入系统查找路径中:

1
2
3
sudo vim /etc/ld.so.conf.d/libc.conf 
将目录/usr/local/lib写入该文件中
执行sudo ldconfig

7 安装完成

  • 安装json库

    1.下载JsonCpp:http://sourceforge.net/projects/jsoncpp/files/
    2.下载scons:http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download
    3.解压scons-2.1.0.tar.gz:tar -zvxf scons-2.1.0.tar.gz
    4.进入解压目录scons-2.1.0,执行命令:sudo python setup.py install
    5.解压jsoncpp:tar -zvxf jsoncpp-src-0.5.0.tar.gz
    6.进入jsoncpp解压目录下,执行命令:sudo scons platform=linux-gcc
    7.将/jsoncpp-src-0.5.0/include/目录下的json文件夹拷贝到/usr/include/
    8.将/jsoncpp-src-0.5.0/libs/linux-gcc-4.9.1/目录下的libjson_linux-gcc-4.9.1_libmt.so和libjson_linux-gcc-4.9.1_libmt.a 拷贝到/usr/local/lib/,并为了方便使用,将其重命名为libjson.so

三、编写代码

代码名称:getInfo.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>
#include <json/json.h>
#include <iconv.h>
#include <iostream>
#include <sstream>
using namespace std;

size_t push_string(void* buffer, size_t size, size_t nmemb, void* stream)
{
string data((const char*)buffer, (size_t) size * nmemb);
*((stringstream*) stream) << data << endl;
return size*nmemb;
}

char *send_post(char *url, char *param)
{
std::stringstream res_str;

CURL *curl_handle = NULL;
CURLcode curl_res;
curl_res = curl_global_init(CURL_GLOBAL_ALL);

// printf("param is: %s\n", param);
if(curl_res == CURLE_OK)
{
curl_handle = curl_easy_init();
if(curl_handle != NULL)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(param));
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, param);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, push_string);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &res_str);
curl_easy_setopt(curl_handle, CURLOPT_HEADER, 0L);

struct curl_slist *pList = NULL;
pList = curl_slist_append(pList,"Content-Type: application/json;charset=utf-8");

curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, pList);
curl_res = curl_easy_perform(curl_handle);
if(curl_res != CURLE_OK)
{
printf("curl_easy_perform error, err_msg:[%ld]\n", curl_res);
}
curl_easy_cleanup(curl_handle);
}
}
else
{
printf("CURL ERROR : %s", curl_easy_strerror(curl_res));
}

std::string str_json = res_str.str();
char *str;
str = (char *)malloc(200);
strcpy(str, str_json.c_str());
return str;
}

//解析json格式的返回值
void get_ret_info(char *res_str, char *flag, char *password, char *msg)
{
Json::Reader json_reader;
Json::Value json_value;
if(json_reader.parse(res_str, json_value))
{
std::string flag1 = json_value["id"].asString();
std::string password1 = json_value["password"].asString();
std::string msg1 = json_value["msg"].asString();

strcpy(flag, flag1.c_str());
strcpy(password, password1.c_str());
strcpy(msg, msg1.c_str());
}
}

int main(int argc, char *argv[])
{
char url[100] = "http://172.16.10.138:8888/ServerX/doRecog"; //服务端url
char param[500] = {0}; //输入参数
char *res_str; //返回数据

//构造json格式的参数
Json::Value item;
item["username"] = Json::Value("test");
item["paramdata"] = Json::Value("==2==NULL==");
item["signdata"] = Json::Value("NULL");
item["imgtype"] = Json::Value("jpg");

std::string str = item.toStyledString();
strcpy(param, );

res_str = send_post(url, (char*)str.c_str());
printf("return string is: %s", res_str);

char flag[10] = {0};
char password[30] = {0};
char msg[200] = {0};
get_ret_info(res_str, flag, password, msg);
}

三、编译

  • 手动编译

    1
    2
    g++ -c getInfo.cpp -o getInfo.o
    g++ -o getInfo.exe -L /usr/local/lib -lcurl -ljson getInfo.o
  • 自动编译

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #x86 complie config
    CC=g++
    LD=g++
    CFLAGS=-Wall -DLDAP_DEPRECATED=1 -I ./include/

    ARCH=$(shell getconf LONG_BIT)

    ifeq ($(DBGEN),1)
    CFLAGS += -g
    endif

    ifeq ($(ARCH),32)
    LIBDIR = /usr/local/lib/
    BINDIR = ./
    CFLAGS += -Dx86
    else
    LIBDIR = /usr/local/lib/
    BINDIR = ./
    endif

    BINLIBS=-L $(LIBDIR) -lcurl -ljson
    EXENAME1=getPasswd
    DEBUG=
    EXEEXT=.exe

    TARGETBIN1 = $(EXENAME1)$(DEBUG)$(EXEEXT)


    BINOBJS1 = getPasswd.o

    all: $(TARGETBIN1)

    .cpp.o:
    $(CC) $(CFLAGS) $(XFLAGS) -c $< -o $@

    .cpp.b:
    $(CC) $(CFLAGS) $(XFLAGS) -c $< -o $@

    $(TARGETBIN1): $(BINOBJS1)
    $(LD) -o $(BINDIR)$(TARGETBIN1) $(BINOBJS1) $(BINLIBS)

    clean:
    rm -f *.o $(BINDIR)$(TARGETBIN1)

#

四、参考文章

  1. 安装curl:https://blog.csdn.net/makenothing/article/details/39250491
  2. 安装json库:https://blog.csdn.net/makenothing/article/details/39250491
  3. https://blog.csdn.net/weixin_39568041/article/details/83659649
  4. https://www.cnblogs.com/chechen/p/7261607.html
  5. https://blog.csdn.net/g1269420003/article/details/89349569
Jeff wechat
------ 版权信息 ------

本文标题:Linux环境下C++访问web服务——使用libcurl库调用http接口发送解析json数据

文章作者:Jeff

发布时间:2019年12月10日 - 18:35

最后更新:2019年12月10日 - 19:15

原始链接:http://JeffCheng95.github.io/2019/12/10/C-通过libcurl库调用http接口/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。