lazarus开发应用提供http接口

本文介绍了使用Lazarus框架中的fphttpapp实现HTTP接口,通过procedureroute1和procedureroute2定义了两个处理函数,并利用TWebServerThread在有界面应用中异步启动HTTP服务。

lazarus开发应用提供http接口,简单试用了一下fphttpapp,发现非常易用,直接支持中文内容

引用3个核心单元

fphttpapp, httpdefs, httproute

启用端口定义路由

procedure route1(aReq: TRequest; aResp: TResponse);
begin
  aResp.Content:='<html><head><title>欢迎测试</title></head><body><h1>Route 1 The Default</h1>欢迎测试</body></html>';
end;

procedure route2(aReq: TRequest; aResp: TResponse);
begin
  aResp.Content:='<html><body><h1>lazarus提供http接口</h1></body></html>';
end;

procedure TForm1.StaticText1Click(Sender: TObject);
begin
HTTPRouter.RegisterRoute('', @route1);
   HTTPRouter.RegisterRoute('/', @route1);
  HTTPRouter.RegisterRoute('/2', @route2);
  fphttpapp.Application.Port := 8088;
  fphttpapp.Application.Threaded := true;
  fphttpapp.Application.Initialize;
//fphttpapp.Application.Run;
 // Run the server in a thread.
  TWebServerThread.Create(false); // false means the server thread runs immediately

end;

如果是无界面应用可以直接启动,这里是有界面应用,所以再写一个线程启动监听更加好用

下面是定以线程和线程启动http服务

type
     TWebServerThread = class(TThread)
    protected
      procedure Execute; override;
    public
      constructor Create(CreateSuspended: boolean);
  end;
  { TForm1 }
......
implementation

{$R *.lfm}

constructor TWebServerThread.Create(CreateSuspended: boolean);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := true;
end;

procedure TWebServerThread.Execute;
begin
       writeln(' fphttpapp.Application.Run');
  fphttpapp.Application.Run;
end;

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值