使用poco库搭建简单http服务器实现hello world
来源:互联网 发布:李剑模糊阅读知乎 编辑:程序博客网 时间:2024/06/03 22:53
源代码例子如下:
#include "Poco/Net/HTTPServer.h"#include "Poco/Net/HTTPRequestHandler.h"#include "Poco/Net/HTTPRequestHandlerFactory.h"#include "Poco/Net/HTTPServerParams.h"#include "Poco/Net/HTTPServerRequest.h"#include "Poco/Net/HTTPServerResponse.h"#include "Poco/Net/HTTPServerParams.h"#include "Poco/Net/ServerSocket.h"#include "Poco/Util/ServerApplication.h"#include <iostream>using Poco::Net::ServerSocket;using Poco::Net::HTTPRequestHandler;using Poco::Net::HTTPRequestHandlerFactory;using Poco::Net::HTTPServer;using Poco::Net::HTTPServerRequest;using Poco::Net::HTTPServerResponse;using Poco::Net::HTTPServerParams;using Poco::Util::ServerApplication;using Poco::Util::Application;/*poco提供了HTTPServer类创建一个http服务器官网介绍:TCPServer的子类,实现了全功能的多线程HTTP服务器。 必须提供HTTPRequestHandlerFactory。 ServerSocket必须绑定并处于侦听状态。 要配置服务器的各个方面,可以将HTTPServerParams对象传递给构造函数HTTPServer有3个构造函数,用其中一个测试就行HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams)*//*HTTPRequestHandler官网介绍:Derived classes must override the handleRequest() method. Furthermore, a HTTPRequestHandlerFactory must be provided翻译:派生类必须覆盖handleRequest()方法。此外,必须提供HTTPRequestHandlerFactoryThe handleRequest() method must perform the complete handling of the HTTP request connection.As soon as the handleRequest() method returns, the request handler object is destroyed.A new HTTPRequestHandler object will be created for each new HTTP request that is received by the HTTPServer.翻译:handleRequest()方法必须执行HTTP请求连接的完整处理。 一旦handleRequest()方法返回,请求处理程序对象就被销毁。 将为HTTPServer接收的每个新HTTP请求创建一个新的HTTPRequestHandler对象。handleRequest()函数功能: virtual void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) = 0 Must be overridden by subclasses:必须被子类覆盖 Handles the given request:处理给定的请求*/class RequestHandLer :public HTTPRequestHandler{public:void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response){response.setChunkedTransferEncoding(true);//设置分组传输编码response.setContentType("text/html");//设置内容类型std::ostream& ostr = response.send();//这个函数返回一个响应请求的输出流ostr << "<h1>Hello World!</h1>";}};class RequestHandlerFactory :public HTTPRequestHandlerFactory{public:/*为给定的HTTP请求创建一个新的请求处理程序,参数是HTTPServerRequest,上面要定义一个HTTPServerRequest对象传过来该方法应该检查给定的HTTPServerRequest对象(例如方法)和URI),并创建一个适当的HTTPRequestHandler对象来处理请求*/HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request){return new RequestHandLer();}};/*创建一个应用程序ServerApplicationHTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams)*/class Myapp :public ServerApplication{protected:int main(const std::vector<std::string>& args){HTTPServer HSer(new RequestHandlerFactory, ServerSocket(8080), new HTTPServerParams);// ServerSocket是匿名对象HSer.start();//启动http服务器waitForTerminationRequest();//std::cout << "关闭" << std::endl;HSer.stop();return Application::EXIT_OK;}};int main(int argc, char** argv){Myapp app;return app.run(argc, argv);}
运行结果如下:打开浏览器输入:localhost:8080 请求,得到hello world
阅读全文
0 0
- 使用poco库搭建简单http服务器实现hello world
- Poco库实现简单的http服务器
- 使用C#开发HTTP服务器系列之Hello World
- 使用C#开发HTTP服务器系列之Hello World
- Jetty:Hello World,使用main启动的简单http server
- Eclipse使用maven搭建简单spring boot application Hello World
- 使用Poco库编写一个简单的http客户端原理
- nginx 编写简单HTTP模块 hello world
- 搭建简单的OSGI的hello world
- spring boot(1)-简单搭建 Hello World
- 使用Python搭建简单Http服务器
- Poco教程(一):使用Poco库建立Http Server
- POCO::Net 简单的HTTP客户端,服务器程序
- 简单的 Nodejs jade 实现Hello world
- 实现vs2013客户端服务器之间的简单通信,hello world 程序。
- 使用Java语言实现简单HTTP服务器
- rabbitMQ实战(一)---------使用pika库实现hello world
- poco库的简单实现单元测试
- GC面试心得
- JDK8较JDK7部分新特性
- windows下git使用
- 3374-数据结构实验之查找二:平衡二叉树
- Servlet.service() for servlet [springServlet] in context with path [/TESTVIDEO] threw exception [Req
- 使用poco库搭建简单http服务器实现hello world
- 3375-数据结构实验之查找三:树的种类统计
- 一致性哈希算法
- Java中的锁分类
- 3642-判断给定森林中有多少棵树特别版
- 神经网络算法是数据拟合算法
- 神经网络实现手写字符识别系统
- 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号
- apk图片修改方案