使用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
原创粉丝点击