📄 httpasy1.pas
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Creation: January 31, 1998
Version: 1.02
Description: This program shows how to use the THttpCli component to execute
a list of request sequentialy or simultaneously.
Simultaneous request are possible without using threads because
the THttpCli component is fully event-driven and asynchronous.
Data is not stored. See the HttpTst sample program for an
example of data store. We keep this sample as simple as possible.
EMail: francois.piette@overbyte.be http://www.overbyte.be
francois.piette@rtfm.be http://www.rtfm.be/fpiette
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1998-2005 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@overbyte.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
Updates:
Feb 01, 1998 V1.01 Adedpted to be compatible with Delphi 1
Save window position and size to ini file.
Jan 10, 2004 V1.02 Steve Endicott <Endi@pacbell.net> added a feature to
execute head of get at will. Just add 'head ' in front of the URL
to execute a head request. Just the URL will result in a get.
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit HttpAsy1;
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, HttpProt, IniFiles;
const
HttpAsyVersion = 102;
CopyRight : String = 'HttpTst (c) 1998-2005 Francois Piette V1.02 ';
type
THttpAsyForm = class(TForm)
URLListBox: TListBox;
Panel1: TPanel;
Label1: TLabel;
URLEdit: TEdit;
AddButton: TButton;
Panel2: TPanel;
ExecButton: TButton;
RemoveButton: TButton;
DisplayMemo: TMemo;
HttpCli1: THttpCli;
ReplaceButton: TButton;
ClearDisplayButton: TButton;
HeaderCheckBox: TCheckBox;
DataCheckBox: TCheckBox;
SimultCheckBox: TCheckBox;
AbortButton: TButton;
Label7: TLabel;
Label8: TLabel;
HttpVersionComboBox: TComboBox;
procedure AddButtonClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure RemoveButtonClick(Sender: TObject);
procedure ExecButtonClick(Sender: TObject);
procedure HttpCli1RequestDone(Sender: TObject; RqType: THttpRequest;
Error: Word);
procedure URLListBoxClick(Sender: TObject);
procedure ReplaceButtonClick(Sender: TObject);
procedure ClearDisplayButtonClick(Sender: TObject);
procedure HttpCli1HeaderData(Sender: TObject);
procedure HttpCli1DocData(Sender: TObject; Buffer: Pointer;
Len: Integer);
procedure AbortButtonClick(Sender: TObject);
private
FInitialized : Boolean;
FIniFileName : String;
FCurrentItem : Integer;
FHttpCliList : TList; { For simultaneous requests }
FFlagAbort : Boolean;
procedure StartNext; { For sequential requests }
procedure ExecSimultaneous;
procedure ExecSequential;
procedure HttpCliItemRequestDone(Sender: TObject; { For simult. requests }
RqType: THttpRequest; Error: Word);
end;
var
HttpAsyForm: THttpAsyForm;
implementation
{$R *.DFM}
const
SectionData = 'Data';
KeyURL = 'URLEdit';
KeyCount = 'Count';
KeyList = 'List';
KeySimult = 'Simultaneous';
KeyHeader = 'DisplayHeader';
KeyData = 'DisplayData';
KeyHttpVer = 'HttpVer';
SectionWindow = 'Window';
KeyTop = 'Top';
KeyLeft = 'Left';
KeyWidth = 'Width';
KeyHeight = 'Height';
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimRight(Str : String) : String;
var
i : Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] = ' ') do
i := i - 1;
Result := Copy(Str, 1, i);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
while (i <= Length(Str)) and (Str[i] = ' ') do
i := i + 1;
Result := Copy(Str, i, Length(Str) - i + 1);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Trim(Str : String) : String;
begin
Result := TrimLeft(TrimRight(Str));
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpAsyForm.FormCreate(Sender: TObject);
begin
DisplayMemo.Clear;
FCurrentItem := -1;
FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
FHttpCliList := TList.Create;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpAsyForm.FormShow(Sender: TObject);
var
IniFile : TIniFile;
Count : Integer;
I : Integer;
URL : String;
begin
if not FInitialized then begin
FInitialized := TRUE;
IniFile := TIniFile.Create(FIniFileName);
URLEdit.Text := IniFile.ReadString(SectionData, KeyURL, '');
HeaderCheckBox.Checked := (IniFile.ReadInteger(SectionData, KeyHeader, 0) <> 0);
DataCheckBox.Checked := (IniFile.ReadInteger(SectionData, KeyData, 0) <> 0);
SimultCheckBox.Checked := (IniFile.ReadInteger(SectionData, KeySimult, 0) <> 0);
Count := IniFile.ReadInteger(SectionData, KeyCount, 0);
for I := 1 to Count do begin
URL := IniFile.ReadString(SectionData, KeyList + IntToStr(I), '');
if URL <> '' then
URLListBox.Items.Add(URL);
end;
HttpVersionComboBox.ItemIndex := IniFile.ReadInteger(SectionData, KeyHttpVer, 0);
Top := IniFile.ReadInteger(SectionWindow, KeyTop, Top);
Left := IniFile.ReadInteger(SectionWindow, KeyLeft, Left);
Width := IniFile.ReadInteger(SectionWindow, KeyWidth, Width);
Height := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);
IniFile.Free;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpAsyForm.FormClose(Sender: TObject;
var Action: TCloseAction);
var
IniFile : TIniFile;
I : Integer;
Count : Integer;
begin
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteString(SectionData, KeyURL, URLEdit.Text);
IniFile.WriteInteger(SectionData, KeyHeader, Ord(HeaderCheckBox.Checked));
IniFile.WriteInteger(SectionData, KeyData, Ord(DataCheckBox.Checked));
IniFile.WriteInteger(SectionData, KeySimult, Ord(SimultCheckBox.Checked));
Count := URLListBox.Items.Count;
IniFile.WriteInteger(SectionData, KeyCount, Count);
for I := 1 to Count do
IniFile.WriteString(SectionData, KeyList + IntToStr(I),
URLListBox.Items[I - 1]);
IniFile.WriteInteger(SectionData, KeyHttpVer, HttpVersionComboBox.ItemIndex);
IniFile.WriteInteger(SectionWindow, KeyTop, Top);
IniFile.WriteInteger(SectionWindow, KeyLeft, Left);
IniFile.WriteInteger(SectionWindow, KeyWidth, Width);
IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
IniFile.Destroy;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Add an URL to the URL list box }
procedure THttpAsyForm.AddButtonClick(Sender: TObject);
begin
if Trim(URLEdit.Text) <> '' then
URLListBox.Items.Add(URLEdit.Text);
ActiveControl := URLEdit;
URLEdit.SelectAll;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Remove the selected URL from hte URL listbox }
procedure THttpAsyForm.RemoveButtonClick(Sender: TObject);
var
Item : Integer;
begin
Item := URLListBox.ItemIndex;
if Item >= 0 then
URLListBox.Items.Delete(Item);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -