使用poco库搭建简单http服务器实现hello world
2017-09-04 19:49
423 查看
源代码例子如下:
运行结果如下:打开浏览器输入:localhost:8080 请求,得到hello world
#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()方法。此外,必须提供HTTPRequestHandlerFactory The 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(); } }; /* 创建一个应用程序ServerApplication HTTPServer(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
相关文章推荐
- 使用Python搭建简单Http服务器
- 用Python实现简单的HTTP服务器(1)--使用Firebug简单分析HTTP协议
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——基础环境搭建
- 使用Java语言实现简单HTTP服务器
- j2ee 简单网站搭建:(七)使用 shiro 结合 jcaptcha 实现用户验证登录
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——基础环境搭建
- j2ee 简单网站搭建:(八)使用 jquery-validate 实现页面验证入门
- CAS 实现单点登录(一):环境搭建、部署及简单使用
- Poco库实现简单的http服务器
- 使用Netty实现的一个简单HTTP服务器
- 使用C#开发HTTP服务器系列之更简单的实现方式
- 2016windows(10) wamp 最简单30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world
- j2ee 简单网站搭建:(六)使用 hibernate validation 实现 domain 层实体类验证
- nginx学习笔记(1) 使用nginx搭建简单的HTTP服务器
- node使用express搭建简单web框架并实现文件上传
- 使用J2ME技术实现简单的动画(附代码实现)
- Spring--简单使用quartz实现定时作业
- 使用System.DirectoryServices.Protocols实现对AD的简单操作
- Asp.Net使用POST方法最简单的实现
- 使用异或进行简单的密码加密(JAVA实现)