如何配置 http_listener 来监听我的 ip 地址以便其他计算机 在网络上可以向服务器发送请求吗?
http_listener listener(L"http://localhsot:9000"); // not working
http_listener listener(L"http://0.0.0.0:9000"); // run time error
http_listener listener(L"http://*:9000"); // run time error
我想在本地网络上使用 c++ rest sdk 作为服务器。
最佳答案
之所以http_listener listener(L"http://localhsot:9000");
不工作是因为“localhost”拼写错误。
纠正拼写错误后,您应该能够将网络浏览器指向 http://localhost:9000你会收到请求。使用打印功能对其进行测试。
如前所述,不要忘记设置对请求的支持。
Listener.support(methods::GET, std::bind(&HandleGet, this, std::placeholders::_1));
和 HandleGet 函数(如果有 GET 请求)
HandleGet(http_request request)
{
std::wcout << L"Received GET request" << std::endl;
}
所以在设置完成后,将您的网络浏览器指向该地址,您应该会看到输出。
此外,您可以将 ServerInit.open().wait()
(开始监听)包装在 try/catch
中,看看它为什么不工作。
关于c++ rest sdk http_listener 作为网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20817648/