使用poco库搭建简单http服务器实现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



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值