【问题标题】:c++ rest sdk http_listener as a network serverc++ rest sdk http_listener 作为网络服务器
【发布时间】:2013-12-28 18:03:04
【问题描述】:

如何配置 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 作为服务器。

【问题讨论】:

    标签: c++ rest httplistener casablanca


    【解决方案1】:

    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 中,看看它为什么不起作用。

    【讨论】:

      【解决方案2】:

      作为作者,我强烈推荐Restbed。它是一个用 C++11 编写的开源项目,目标是达到 HTTP(s) 1.0-2.0 合规性。

      #include <memory>
      #include <cstdlib>
      #include <restbed>
      
      using namespace std;
      using namespace restbed;
      
      void get_method_handler( const shared_ptr< Session >& session )
      {
          const auto request = session->get_request( );
      
          size_t content_length = 0;
          request->get_header( "Content-Length", content_length );
      
          session->fetch( content_length, [ ]( const shared_ptr< Session >& session,
                                               const Bytes& body )
          {
              fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
      
              session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
          } );
      }
      
      int main( const int, const char** )
      {
          auto resource = make_shared< Resource >( );
          resource->set_path( "/resource" );
          resource->set_method_handler( "GET", get_method_handler );
      
          auto settings = make_shared< Settings >( );
          settings->set_port( 1984 );
          settings->set_default_header( "Connection", "close" );
      
          Service service;
          service.publish( resource );
          service.start( settings );
      
          return EXIT_SUCCESS;
      }
      

      更多示例请参见here

      【讨论】:

        【解决方案3】:

        https://casablanca.codeplex.com/discussions/478976

        HENRYTM 答案

        您好,我也遇到了这个问题。 您可以通过在 linux 上使用 nginx 作为代理来解决此问题。这也可以为您的服务器添加 https 功能。 这是所需的 nginx 配置文件的一个小版本:

           server {
            listen *:80;
            server_name localhost;
        
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://localhost:12345/;
                    proxy_redirect http://localhost:12345/ https://$server_name/;
          }
        

        此脚本将所有流量重定向到 12345。现在您只需收听

           http_listener listener(L"http://localhost:12345");
        

        不要忘记“支持”电话。 干杯, 亨利

        【讨论】:

        • 有谁知道如何在不使用代理的情况下在卡萨布兰卡做到这一点?我想使用 Casablanca 作为嵌入式 http 服务器。
        【解决方案4】:

        我在 dyndns.it 上创建了一个帐户,并使用 dynds.it 帐户的用户名和密码设置我的路由器动态 dns 帐户。 在该服务器侦听该 url 之后:

        uri_builder uri(L"http://my_account.homepc.it:80/"); 
        auto addr = uri.to_string();
        http_listener listener(addr);
        
        
        
        listener.support(methods::POST, handle_post);
        
        try
        {
            listener
                .open()
                .then([&listener](){TRACE(L"\nstarting to listen\n"); })
                .wait();
        
            while (true);
        }
        catch (exception const & e)
        {
            wcout << e.what() << endl;
        }
        catch (...)
        {
            wcout << "error" << endl;
        }
        

        它可以在任何位置工作(即使我仍然不明白如何同时管理来自多个客户端的 POST!)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-28
          • 2016-04-01
          • 2013-08-20
          • 2015-01-31
          • 1970-01-01
          相关资源
          最近更新 更多