实现功能:启动一个Http服务。
如下图所示建立工程:
代码如下:
在C盘下建立文本文件abc.txt,内容如下:
启动Delphi程序,访问如下的地址即可: http://localhost:6001/
如下图所示建立工程:
代码如下:
- unit HttpUnit;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
- IdHTTPServer, IdContext;
- type
- TForm1 = class(TForm)
- IdHTTPServer1: TIdHTTPServer;
- procedure FormCreate(Sender: TObject);
- procedure IdHTTPServer1CommandGet(AContext: TIdContext;
- ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
- private
- { Private declarations }
- public
- { Public declarations }
- protected
- FData: string;
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- type
- TSlowStream = class(TFileStream)
- public
- function Read(var Buffer; Count: Longint): Longint; override;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- var
- i: Integer;
- begin
- IdHTTPServer1.DefaultPort := 6001;
- IdHTTPServer1.Active := True;
- SetLength(FData, 1024);
- for i := 1 to Length(FData) do begin
- FData[i] := Chr(i mod 256);
- end;
- end;
- function TSlowStream.Read(var Buffer; Count: Integer): Longint;
- begin
- Result := inherited Read(Buffer, Count);
- Sleep(Count div 2);
- end;
- procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
- ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
- begin
- AResponseInfo.ContentStream := TSlowStream.Create('C:/abc.txt', fmOpenRead);
- end;
- end.
- sdasdasd
- <br>
- asdasd
- <br>
- <input type="text" value="aaa">