function TFormMain.GetTHTTPClient (
Const xURL :
String ) :
String;
var
tmp :
String;
MyThread: System.Classes.TThread;
begin
tmp := '
';
CancelThread := False;
MyThread := System.Classes.TThread.CreateAnonymousThread(
procedure
var
HttpClient: System.Net.HttpClient.THttpClient;
HttpResponse: System.Net.HttpClient.IHttpResponse;
begin
HttpClient := System.Net.HttpClient.THTTPClient.Create;
try
HttpClient.UserAgent := '
Mozilla/4.0 (compatible; MSIE 7.0; Windows; U; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 */*';
HttpClient.MaxRedirects := 10;
HttpClient.HandleRedirects := True;
HttpClient.ContentType := '
*/*';
HttpClient.Accept := '
*/*';
HttpClient.ResponseTimeout := 5000;
HttpClient.ConnectionTimeout := 5000;
try
HttpResponse := HttpClient.Get( xURL );
tmp := HttpResponse.ContentAsString();
except
on e: System.SysUtils.Exception
do
tmp := '
Error Occured @ '+xURL+'
- '+e.
Message;
end;
finally
HttpClient.Free;
end;
end
);
MyThread.FreeOnTerminate := True;
MyThread.Start;
repeat
Vcl.Forms.Application.ProcessMessages;
System.SysUtils.Sleep(5);
if CancelThread
then
begin
CancelThread := False;
tmp := '
Download aborted.';
if not Winapi.Windows.TerminateProcess(MyThread.Handle, 0)
then MyThread.Terminate;
end;
until ((tmp <> '
')
or (
Winapi.Windows.WaitForSingleObject(MyThread.Handle, 5) =
Winapi.Windows.WAIT_FAILED));
// WAIT_FAILED = DWORD($FFFFFFFF);
if tmp = '
'
then tmp := '
Error Occured @ '+xURL;
Result := tmp;
end;