新建Webbroker项目:
选择类型:
开始可以使用:Indy VCL Application 方便调试,完成后,再新建一个DLL 项目,引用业务单元。
示例代码如下:
unit uDataModuleMain;
interface
uses
SysUtils, Classes, HTTPApp, HTTPProd;
type
TWebModule2 = class(TWebModule)
ppTest: TPageProducer;
procedure ppTestHTMLTag(Sender: TObject; Tag: TTag; const TagString: string;
TagParams: TStrings; var ReplaceText: string);
procedure WebModule2DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TWebModule2;
implementation
{$R *.dfm}
procedure TWebModule2.ppTestHTMLTag(Sender: TObject; Tag: TTag;
const TagString: string; TagParams: TStrings; var ReplaceText: string);
begin
Response.ContentType := 'text/html;charset="utf-8"';
try
if SameText(TagString, 'doctitle') then // TPageProducer组件的标记处理,指定 HTMLDoc 属性 ,标记格式 <#flag>
ReplaceText := dm.GetTitle
else if SameText(TagString, 'docbody') then
ReplaceText := dm.GetBody
else
ReplaceText := '';
except
on E: Exception do
begin
ReplaceText := Format('ERROR! <#%s> raised %s: %s',
[TagString, E.ClassName, E.Message])
end
end;
end;
procedure TWebModule2.WebModule2DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentType := 'text/html;charset="utf-8"'; //指定编码
Response.Content := 'WEB服务运行正常. ' + DateTimeToStr(Now);
// Request.QueryFields.Values['sid']; //查询参数获取
end;
end.