@@ -41,6 +41,7 @@ interface
4141 { TMainForm }
4242
4343 TMainForm = class (TForm)
44+ FetchBtn: TButton;
4445 StopBtn: TButton;
4546 DownloadBtn: TButton;
4647 DownloadProgressLabel: TLabel;
@@ -52,11 +53,12 @@ TMainForm = class(TForm)
5253 ProgressStaticLabel: TLabel;
5354 URLEdit: TEdit;
5455 UrlStaticLabel: TLabel;
56+ procedure FetchBtnClick (Sender: TObject);
5557 procedure StopBtnClick (Sender: TObject);
5658 procedure DownloadBtnClick (Sender: TObject);
5759 procedure FormDestroy (Sender: TObject);
5860 procedure UpdateProgress (ALen, APos: Int64; AHRLen, AHRPos: String);
59- procedure UpdateStatus (Status: TStatus; ResponseCode: Integer; Msg: String);
61+ procedure UpdateStatus (Status: TStatus; ResponseCode: Integer; Msg: String; Body: String );
6062 private
6163 LazSimpleHTTPsGet: TLazSimpleHTTPsGet;
6264 public
@@ -78,9 +80,12 @@ procedure TMainForm.DownloadBtnClick(Sender: TObject);
7880 Filename: String;
7981begin
8082 try
81- if not Assigned(LazSimpleHTTPsGet) then
83+ if Length(URLEdit.Text) > 0 then
8284 begin
83- LazSimpleHTTPsGet := TLazSimpleHTTPsGet.Create();
85+ if not Assigned(LazSimpleHTTPsGet) then
86+ begin
87+ LazSimpleHTTPsGet := TLazSimpleHTTPsGet.Create();
88+ end ;
8489
8590 Filename := LazSimpleHTTPsGet.GetFileNameFromURL(URLEdit.Text);
8691 SaveDialog.FileName := Filename;
@@ -90,14 +95,20 @@ procedure TMainForm.DownloadBtnClick(Sender: TObject);
9095 LazSimpleHTTPsGet.Filename := SaveDialog.FileName;
9196 LazSimpleHTTPsGet.OnProgress := @UpdateProgress;
9297 LazSimpleHTTPsGet.onStatus := @UpdateStatus;
93- LazSimpleHTTPsGet.Start;
94- DownloadBtn.Enabled := False;
95- StopBtn.Enabled := True;
98+ if LazSimpleHTTPsGet.Get then
99+ begin
100+ DownloadBtn.Enabled := False;
101+ StopBtn.Enabled := True;
102+ end
103+ else
104+ begin
105+ ShowMessage(' Download already running' );
106+ end ;
96107 end ;
97108 end
98109 else
99110 begin
100- ShowMessage(' Download already running ' );
111+ ShowMessage(' Please enter a url ' );
101112 end ;
102113 except
103114 on E: Exception do
@@ -112,7 +123,6 @@ procedure TMainForm.StopBtnClick(Sender: TObject);
112123begin
113124 if Assigned(LazSimpleHTTPsGet) then
114125 begin
115- LazSimpleHTTPsGet.Stop;
116126 StatusInfoMemo.Append(' Download stopped' );
117127 FreeAndNil(LazSimpleHTTPsGet);
118128 DownloadBtn.Enabled := True;
@@ -122,12 +132,46 @@ procedure TMainForm.StopBtnClick(Sender: TObject);
122132 end ;
123133end ;
124134
135+ procedure TMainForm.FetchBtnClick (Sender: TObject);
136+ begin
137+ try
138+ if Length(URLEdit.Text) > 0 then
139+ begin
140+ if not Assigned(LazSimpleHTTPsGet) then
141+ begin
142+ LazSimpleHTTPsGet := TLazSimpleHTTPsGet.Create();
143+ end ;
144+
145+ LazSimpleHTTPsGet.URL := URLEdit.Text;
146+ LazSimpleHTTPsGet.OnProgress := @UpdateProgress;
147+ LazSimpleHTTPsGet.onStatus := @UpdateStatus;
148+ if LazSimpleHTTPsGet.Fetch then
149+ begin
150+ DownloadBtn.Enabled := False;
151+ StopBtn.Enabled := True;
152+ end
153+ else
154+ begin
155+ ShowMessage(' Download already running' );
156+ end ;
157+ end
158+ else
159+ begin
160+ ShowMessage(' Please enter a url' );
161+ end ;
162+ except
163+ on E: Exception do
164+ begin
165+ ShowMessage(E.Message);
166+ end ;
167+ end ;
168+ end ;
169+
125170// Form closed, stop downloads and free and nil LazSimpleHTTPsGet:
126171procedure TMainForm.FormDestroy (Sender: TObject);
127172begin
128173 if Assigned(LazSimpleHTTPsGet) then
129174 begin
130- LazSimpleHTTPsGet.Stop;
131175 FreeAndNil(LazSimpleHTTPsGet);
132176 end ;
133177end ;
@@ -146,7 +190,7 @@ procedure TMainForm.UpdateProgress(ALen, APos: Int64; AHRLen, AHRPos: String);
146190end ;
147191
148192// Status event (download start, done, error):
149- procedure TMainForm.UpdateStatus (Status: TStatus; ResponseCode: Integer; Msg: String);
193+ procedure TMainForm.UpdateStatus (Status: TStatus; ResponseCode: Integer; Msg: String; Body: String );
150194begin
151195 if Status = TStatus.sStart then
152196 begin
@@ -155,6 +199,14 @@ procedure TMainForm.UpdateStatus(Status: TStatus; ResponseCode: Integer; Msg: St
155199 else if Status = TStatus.sDone then
156200 begin
157201 StatusInfoMemo.Append(' Download done -> (HTTP Status: ' + IntToStr(ResponseCode) + ' /' + Msg + ' )' );
202+ if Length(Body) > 0 then
203+ begin
204+ StatusInfoMemo.Append(' -- Body Start ------------------' );
205+ StatusInfoMemo.Append(Body);
206+ StatusInfoMemo.Append(' -- Body End --------------------' );
207+ end ;
208+ DownloadBtn.Enabled := True;
209+ StopBtn.Enabled := False;
158210 DownloadProgressLabel.Caption := ' -/-' ;
159211 ProgressBar.Position := 0 ;
160212 end
0 commit comments