Delphi WebBroker【Web服务器应用程序】+简单接口  
官方Delphi 学习QQ群: 682628230(三千人)
频道

Delphi WebBroker【Web服务器应用程序】+简单接口


一、建好项目
1.新建项目 – 其他 – 找到Web服务器应用程序

20210106215837596.png

2.然后会弹出这些界面,基本都直接下一步就好了【这个Demo就是这样这样的】

20210106215837596.png

20210106215837596.png

2.png

下面可以测试一下自己的 8080 端口是否被占用了,占用了就换别的就好了,然后完成【不会影响后面的操作】

20210106215837596.png

二、项目创建完成后
然后就会看到这样的已经成型的东西

20210106215837596.png

然后会发现访问的就是WebModuleUnit1这个单元下最后面返回的内容,接下来就是修改WebModuleUnit1这个单元里面的内容了,没有动FormUnit1单元下面内容
下面的代码是WebModuleUnit1修改后的内容




unit WebModuleUnit1;


interface


uses

  System.SysUtils, System.Classes, Web.HTTPApp;


type

  TWebModule1 = class(TWebModule)

    procedure WebModule1DefaultHandlerAction(Sender: TObject;

      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  WebModuleClass: TComponentClass = TWebModule1;


implementation


{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;

  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);

var

  mPath, mXY: string;

begin

  // 设置返回的类型 【也可以写在这里,下面的就不用写】

  // Response.ContentType := 'application/json; charset="UTF-8"';

  // 获取请求路径

  mPath := Request.PathInfo;

  // 获取请求字段的值

  mXY := Request.QueryFields.Values['XY'];


  // 当访问的是首页的时候【http://localhost:8080/】这个端口号是跟前面窗体上面启动服务的端口号相同的

  if mPath = '/' then

  begin

    Response.ContentType := 'text/html; charset="UTF-8"';

    // 这个下面的是不是很熟悉,这个就是在创建后生成的返回

    Response.Content := '

      '' +

      '' + '';

  end

  // 【http://localhost:8080/xaioyin】

  else if mPath = '/xiaoyin' then

  begin

    // 判断请求的参数是否是符合要求 【http://localhost:8080/xaioyin?XY=xiaoyin01】

    if mXY = 'xiaoyin01' then

    begin

      Response.ContentType := 'application/json; charset="UTF-8"';

      Response.Content := '{"status":200,"Hint":"可以调用就表示成功啦!"}';

    end

    else

    begin

      Response.ContentType := 'application/json; charset="UTF-8"';

      Response.Content := '{"status":201,"Hint":"进来这个里面就表示参数不对给的返回"}';

    end;

  end

  else

  begin

    Response.ContentType := 'text/html; charset="UTF-8"';

    Response.Content := '

      '' +

      '' + '';

  end;

end;


end.

————————————————


原文链接:https://blog.csdn.net/qq_44111597/article/details/112298851



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.03970193862915 seconds