如何在 C++ 中使用带有 SFML 的 http 请求从 node.js 服务器获取数据?

How to get data from a node.js server using a http reqest with SFML in C++?

所以,我目前在 C++ 中的代码是:

#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    // prepare the request
    sf::Http::Request request("/", sf::Http::Request::Get);

    // send the request
    sf::Http http("http://localhost:3000/");
    sf::Http::Response response = http.sendRequest(request);

    // check the status
    std::cout << response.getStatus();

    return 0;
}

并且我使用 node.js 创建了一个 local server,我想获取 json 数据。 上面的代码不起作用,我不知道该怎么做。我得到的错误代码是 1001 (ConnectionFailed)。

有什么想法吗?

我认为您使用的 sf::Http 构造函数有误。

构造函数调用 setHost 不解析端口号。您需要显式传递端口号,如下所示:

sf::Http http("http://localhost", 3000);