从主机向 VirtualBox linux 机器上的服务器 运行 发送 http 请求

Sending http request to server running on a VirtualBox linux machine from the host machine

我目前正在使用 Microsoft 的 cpprestsdk 开发一个名为 Casablanca 的 REST 服务器。

我在使用 Oracle VirtualBox 的 linux 虚拟机上安装了服务器 运行。

我将 VM 设置为使用桥接适配器网络,并且可以成功通过 SSH 连接到机器,所以我知道可以向我的服务器发送 http 请求。

我的服务器端点当前设置为:

http://localhost:4200/api"

请参阅下面我的 main.cpp 中的代码:

int main() {
    cout << "Starting Server" << endl;

    TransactionController server;
    server.setEndpoint("http://localhost:4200/api");
    server.initHandlers();

    try {
        server.openServer().wait();
        cout << "Server listening at: " << server.getEndpoint() << endl;

        // figure out how to keep server running without this?
        while (true);
    }
    catch(exception &e) {
        cout << "--- ERROR DETECTED ---" << endl;
        cout << e.what() << endl;
    }


    // this doesn't get reached bc of the while(true)
    server.closeServer().wait();

    return 0;
}

(我知道这不是最干净的实现,但我只是想把一些东西从头开始,这样我就可以测试功能,如果您有任何问题,请随时评论我如何改进该代码片段说)

因此,如果我登录到我的 VM 并在来宾计算机上执行 curl GET 请求,它会成功完成,并且我会按预期收到响应。

卷曲示例:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:4200/api

现在我的问题是,如何使用 Postman 或 Advanced Rest Client 等 HTTP 客户端从我的主机发出相同的请求?

当我尝试从我的主机在我的来宾机器上查询我的服务器 运行 时,我不确定我会把什么作为请求 URL。

使用 ifconfig 我知道我的来宾机器的 ip 地址是:

10.0.0.157

我可以使用这个地址通过 SSH 进入我的虚拟机,所以我知道这是我的来宾机器的正确地址。

但是,我不知道如何将我的 http 请求发送到 运行 我的服务器这台机器。

我不是网络方面的专家,也不是卡萨布兰卡方面的专家,因此非常感谢任何正确方向的指导或指示。感谢您的宝贵时间!

您已将服务器绑定到本地主机,因此它只能从本地主机访问。 我不知道 TransactionController::setEndpoint 的作用,但您可能需要执行以下操作之一:

server.setEndpoint("http://10.0.0.157:4200/api"); // bind to only 10.0.0.157
server.setEndpoint("http://0.0.0.0:4200/api"); // bind to all ipv4 adresses
server.setEndpoint("http://*:4200/api"); // bind to all addresses

以上哪一个会起作用将取决于 setEndpoint 的实施。