程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 使用 IntraWeb (39)

使用 IntraWeb (39)

編輯:Delphi


在其它服務器腳本語言中熟悉的 Request、Response(THttpRequest、THttpReply) 在 IntraWeb 中算是幕後英雄了, 用戶基本不需要直接操作它們了.

IW 默認 Post 傳值(WebApplication.Request.HttpMethod = hmPost, 只讀), 但像表單字段之類, 在 IW 中直接根據控件名稱讀取就是了, 用不著麻煩 Request.
但如果需要, 可以使用 WebApplication.Request.QueryFields.Values['控件名'] 讀取.

要讀出以 Get 方式傳輸的 Url 變量, 發現在很多時候 WebApplication.Request.QueryFields.Values['變量名'] 並不好用; 經過嘗試, 也不是沒有辦法:
1、笨辦法: 自己解析 WebApplication.Request.Referer;
2、Request 以參數方式出現時往往可以, 譬如 TIWAppForm.OnURLRequest 事件:


{Unit1 中的關鍵代碼}
procedure TIWForm1.IWButton1Click(Sender: TObject);
begin
  WebApplication.GoToURL('Page2.html?AAA=111&BBB=222');
end;

{Unit2 中的關鍵代碼}
procedure TIWForm2.IWAppFormURLRequest(aSender: TIWAppForm; aRequest: THttpRequest); //需 uses IW.HTTP.Request
begin
  IWMemo1.Lines.Add(WebApplication.Request.QueryFields.Values['bbb']);
end;

initialization
  TIWForm2.SetURL('', 'Page2.html');


我想, 按照 IW 的理念, 網頁間互相傳遞的參數應該是用 Session(下篇博客測試下).


有時還要用到運行參數, 輸入運行參數有兩種方法:
1.從代碼(如: WebApplication.RunParams.CommaText := 'AAA=111,BBB=222');
2.從 Server 控制台(如下圖);


讀取運行參數, 如: MyStr := WebApplication.RunParams.Values['AAA'];

THttpRequest 所在單元及繼承鏈:
IW.HTTP.Request.THttpRequest < TObject

主要成員(根據測試結果判斷吧):
property UserAgent: string       //測試結果: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
property Referer: string         //測試結果: http://127.0.0.1:8888/$/?aaa=111&bbb=222; 測試時添加了兩個運行參數
property ScriptName: string      //
property Host: string            //測試結果: 127.0.0.1
property URL: string             //
property PathInfo: string        //測試結果: /$/callback
property RemoteAddr: string      //測試結果: 127.0.0.1
property ServerPort: integer     //測試結果: 8888
property Query: string           //測試結果: callback=...
property HttpMethod: THttpMethod //測試結果: hmPost(其它: hmNone, hmGet, hmPut, hmPost, hmHead)
property IfModifiedSince: string //
property ForwardedFor: string    //
property HttpClientIP: string    //
property AcceptLanguage: string  //測試結果: zh-CN
property HttpsHeader: string     //
property Authorization: string   //
property AuthorizationHeader: string //
property AuthUser: string        //
property AuthPass: string        //
property ServerVariables: TStrings //
property HasContent: Boolean       //測試結果: True
property Files: THttpFileList      //
property QueryString: string       //測試結果: callback=...
function GetCookieValue(const aName: string): string;
function GetRawHeaderValue(const aName: string): string;
function GetServerVariableValue(const aVariableName: string): string;
function GetContentFieldValue( const aContentFieldName: string): string;
procedure SetRewriteURL(const aUrl: string);

property RawHeaders: TStrings //測試結果: 
                              //Accept: */*
                              //Content-Type: application/x-www-form-urlencoded
                              //Referer: http://127.0.0.1:8888/$/
                              //Accept-Language: zh-CN
                              //Accept-Encoding: gzip, deflate
                              //User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
                              //Host: 127.0.0.1:8888
                              //Content-Length: 205
                              //DNT: 1
                              //Connection: Keep-Alive
                              //Cache-Control: no-cache
                              //Cookie: IW_MyApp=rtDemtDbmuncnJHgndGWqueWrKmWodK1otaXrue4mdu_2

property ContentFields: TStrings //測試結果:
                                 //IWBUTTON2=
                                 //IW_Action=IWBUTTON2
                                 //IW_ActionParam=
                                 //IW_FormName=IWForm1
                                 //IW_FormClass=TIWForm1
                                 //IW_LocationHash=
                                 //IW_TrackID_=2
                                 //IW_width=1920
                                 //IW_height=985
                                 //IW_SessionID_=mKrbmdjendG0mZLgndGXoefbn0yWoefbmti2rJngode
                                 //callback=IWBUTTON2.DoOnAsyncClick
                                 //x=48
                                 //y=10
                                 //which=0
                                 //modifiers=

property CookieFields: TStrings  //測試結果: IW_MyApp=ourdmeffmdHgmtK1ndiWodGZoungnKvfmtG5nKiXmue_2

property QueryFields: TStrings //測試結果:
                               //IWBUTTON2=
                               //IW_Action=IWBUTTON2
                               //IW_ActionParam=
                               //IW_FormName=IWForm1
                               //IW_FormClass=TIWForm1
                               //IW_LocationHash=
                               //IW_TrackID_=2
                               //IW_width=1920
                               //IW_height=985
                               //IW_SessionID_=nZKXqtvcrJvbmtiZndjfrKe1nZm3numYmta3ruiXnZG
                               //callback=IWBUTTON2.DoOnAsyncClick
                               //x=52
                               //y=18
                               //which=0
                               //modifiers=

function GetParams: TStrings; //測試結果:
                              //IWBUTTON2=
                              //IW_Action=IWBUTTON2
                              //IW_ActionParam=
                              //IW_FormName=IWForm1
                              //IW_FormClass=TIWForm1
                              //IW_LocationHash=
                              //IW_TrackID_=2
                              //IW_width=1920
                              //IW_height=985
                              //IW_SessionID_=nJDbrdvcrdaZnZiZndm0mJG0mem1ndnerdC1nJaWree
                              //callback=IWBUTTON2.DoOnAsyncClick
                              //x=56
                              //y=15
                              //which=0
                              //modifiers=
                              //IWBUTTON2=
                              //IW_Action=IWBUTTON2
                              //IW_ActionParam=
                              //IW_FormName=IWForm1
                              //IW_FormClass=TIWForm1
                              //IW_LocationHash=
                              //IW_TrackID_=2
                              //IW_width=1920
                              //IW_height=985
                              //IW_SessionID_=nJDbrdvcrdaZnZiZndm0mJG0mem1ndnerdC1nJaWree
                              //callback=IWBUTTON2.DoOnAsyncClick
                              //x=56
                              //y=15
                              //which=0
                              //modifiers=



THttpReply 所在單元及繼承鏈:
IW.HTTP.Reply.THttpReply < TObject

主要成員:
property AllowCaching: Boolean //
property AuthRealm: string     //
property CacheTTL: Integer     //
property Code: Word	       //
property CodeText: string      //
property ContentType: string   //
property Cookies: TCookieList //
property LastModified: TDateTime      //
property Headers: TStrings	      //
property Expires: TDateTime	      //
property DataType: THttpReplyType     //
property ServerPort: Integer	      //
property SendingFile: Boolean	      //
property CacheControlEnabled: Boolean //If FALSE, completely disable cache control header fields ("Expires", "Last-Modified", "Pragma: no-cache" and "Cache-Control: no-cache"). Default = TRUE

constructor Create(aBuffer: PByte; const aSize: Integer)
procedure Commit
procedure WriteString(const aValue: string)
procedure SendRenderStream(aStream: TIWRenderStream)
procedure SendStream(aStream: TStream; const aDisableCacheControl: Boolean)
procedure SendHsmStream(aStream: THsmStream)
procedure SendFile(const aPathname: string; aDelete: Boolean; const aDisableCacheControl: Boolean)
procedure SendCacheFile(const aPathname: string)
procedure SendRedirect(const aURL: string)
procedure ResetReplyType


學其他服務器腳本, 都快習慣了先用 Response 輸出點什麼, 下面測試了在 IW 中通過 Response 做簡單的輸出:
{新建 MyIndex 單元, 從 TContentBase 繼承實現一個 TMyIndex 類}
unit MyIndex;

interface

uses Classes, IW.Content.Base, HTTPApp, IWApplication, IW.HTTP.Request, IW.HTTP.Reply, IWMimeTypes;

type
  TMyIndex = class(TContentBase)
  protected
    function Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): Boolean; override;
  public
    constructor Create; override;
  end;

implementation

{ TMyIndex }

constructor TMyIndex.Create;
begin
  inherited;
  mFileMustExist := False;
end;

function TMyIndex.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): Boolean;
begin
  aReply.ContentType := MIME_HTML;
  aReply.WriteString('<a href="http://www.cnblogs.com/del//del.cnblogs.com">萬一的 Delphi 博客</a>');
  Result := True;
end;

end. //MyIndex.pas End


{在 IWServerControllerBase.OnConfig 中指定為首頁, 並命名為 Index.htm}
uses
  IWInit, IWGlobal, IW.Content.Handlers, MyIndex;

procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  THandlers.AddStartHandler('', 'Index.htm', TMyIndex.Create);
end;


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved