如何发送HTTP请求并取回一个JSON响应C ++升压 [英] How to send http request and retrieve a json response C++ Boost

查看:957
本文介绍了如何发送HTTP请求并取回一个JSON响应C ++升压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个命令行客户端玩井字棋上的服务器。
服务器接受HTTP请求,并返回JSON我的客户。我正在寻找一种快速的方法来发送一个HTTP请求,并使用Boost库收到JSON作为一个字符串。

 例如http请求=HTTP:// ??? / newGame名=大卫
例如JSON响应=\\状态\\:\\好\\,\\ID \\:\\游戏-23 \\,\\信\\:2


解决方案

这适合描述简单的事情:

<大骨节病> 住在Coliru

 的#include&LT;升压/ asio.hpp&GT;
#包括LT&;&iostream的GT;诠释主(){
    提高::系统::错误_ code EC;
    使用空间boost :: ASIO;    //我们需要什么
    io_service对象SVC;
    IP :: TCP ::插座袜子(SVC);
    sock.connect({{},8087}); // HTTP://本地主机:测试8087    // 发送请求
    性病::字符串请求(?GET / newGame名=大卫HTTP / 1.1 \\ r \\ n \\ r \\ n);
    sock.send(缓冲(要求));    //读取响应
    标准::字符串响应;    做{
        焦炭BUF [1024];
        为size_t bytes_transferred = sock.receive(缓冲区(BUF){} EC);
        如果(EC!)response.append(BUF,BUF + bytes_transferred);
    }而(EC!);    //打印和退出
    性病::法院LT&;&LT; 收到的回应:&LT;&LT;响应&LT;&LT; '\\ n;
}

这得到充分反应。你可以用虚拟服务器进行测试:
(也住在Coliru

 的netcat -l本地主机8087&LT;&LT;&LT; '状态:还行,ID:游戏-23,信:2'

这将表明,该请求被接收,并且响应将通过我们的客户code以上写出。

请注意,对于更多的想法,你可以看一下例子的 http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples.html (虽然他们专注于异步通信,因为这是短耳库的主题)

I need to write a command line client for playing tic-tac-toe over a server. the server accepts http requests and sends back json to my client. i am looking for a quick way to send a http request and receive the json as a string using boost libraries.

example http request = "http://???/newGame?name=david"
example json response = "\"status\":\"okay\", \"id\":\"game-23\", \"letter\":2"

解决方案

The simplest thing that fits the description:

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

int main() {
    boost::system::error_code ec;
    using namespace boost::asio;

    // what we need
    io_service svc;
    ip::tcp::socket sock(svc);
    sock.connect({ {}, 8087 }); // http://localhost:8087 for testing

    // send request
    std::string request("GET /newGame?name=david HTTP/1.1\r\n\r\n");
    sock.send(buffer(request));

    // read response
    std::string response;

    do {
        char buf[1024];
        size_t bytes_transferred = sock.receive(buffer(buf), {}, ec);
        if (!ec) response.append(buf, buf + bytes_transferred);
    } while (!ec);

    // print and exit
    std::cout << "Response received: '" << response << "'\n";
}

This receives the full response. You can test it with a dummy server:
(also Live On Coliru):

netcat -l localhost 8087 <<< '"status":"okay", "id":"game-23", "letter":2'

This will show that the request is received, and the response will be written out by our client code above.

Note that for more ideas you could look at the examples http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples.html (although they focus on asynchronous communications, because that's the topic of the Asio library)

这篇关于如何发送HTTP请求并取回一个JSON响应C ++升压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆