首页 > 解决方案 > casablanca http_listener“将 url 添加到 url 组时出错”

问题描述

我目前正在尝试使用 casablanca 实现 REST 接口,但我不断收到“将 url 添加到 url 组时出错”。我真的不知道如何解决这个问题。这是我的主要方法:

    int main(int argc, char* argv[])
{
    InterruptHandler::hookSIGINT();

    Server server;
    server.setEndpoint(L"http", 41004, L"/api/v1");

    try {
        // wait for server initialization...
        server.accept().wait();
        std::wcout << L"Modern C++ Server now listening for requests at: " << server.endpoint() << '\n';

        InterruptHandler::waitForUserInterrupt();

        server.shutdown().wait();
    }
    catch (std::exception & e) {
        std::cerr << e.what() << '\n'; //this is returning "Error adding url to url group"
    }

    system("pause");
}

我现在正试图找出问题可能出在哪里,但我没有走远。我正在设置端点并像这样创建 http_listener(服务器类扩展 BaseController):

void BaseController::setEndpoint(const std::wstring &scheme, const int port, const std::wstring &path) {
        uri_builder endpointBuilder;

        endpointBuilder.set_scheme(scheme);
        endpointBuilder.set_host(L"0.0.0.0");
        endpointBuilder.set_port(port); //41004
        endpointBuilder.set_path(path);

        _listener = http_listener(endpointBuilder.to_uri());
    }

当服务器接受时,正在侦听器上设置支持方法

void Server::initRestOpHandlers() {
    _listener.support(methods::GET, std::bind(&Server::handleGet, this, std::placeholders::_1));
    _listener.support(methods::POST, std::bind(&Server::handlePost, this, std::placeholders::_1));
}

在 http_listener.cpp open() 方法中抛出异常:

pplx::task<void> details::http_listener_impl::open()
{
    // Do nothing if the open operation was already attempted
    // Not thread safe
    if (!m_closed) return pplx::task_from_result();

    if ( m_uri.is_empty() )
        throw std::invalid_argument("No URI defined for listener.");
    m_closed = false;

    return web::http::experimental::details::http_server_api::register_listener(this).then([this](pplx::task<void> openOp)
    {
        try
        {
            // If failed to open need to mark as closed.
            openOp.wait();
        }
        catch(...)
        {
            m_closed = true;
            throw;
        }
        return openOp;
    });
}

我在其他地方找不到任何帮助,我似乎无法弄清楚为什么它无法打开。任何帮助,将不胜感激!谢谢。

标签: c++visual-studiohttplistenercasablancacpprest-sdk

解决方案


好的,我自己能够解决它......我必须在我的 BaseController 中使用 127.0.0.1 作为主机


推荐阅读