|
아래의 델파이 소스를 빌더로 해보려는데 도무지 모르겠네요..
빌더 초보인데 고수님들 꼭 좀 도와주세요..-.-
unit WebRobot;
interface
uses
Classes, SysUtils, Windows, IdURI, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, IdAntiFreeze;
type
TSearchURLEvent = procedure (Sender: TObject; const URL:string) of object;
TURLErrorEvent = procedure (Sender: TObject; const URL, Msg:string) of object;
TWebRobot = class(TThread)
private
FHTTP: TIdHTTP;
FURLErrorEvent: TURLErrorEvent;
FSleepTime: Integer;
FSearchURLEvent: TSearchURLEvent;
procedure UpdateURL(const URL: string);
procedure UpdateError(const Msg: string);
protected
procedure URLCallback;
procedure ErrorCallback;
public
constructor Create; virtual;
destructor Destroy; override;
function GetURLText(const URL: string): string;
public
property SleepTime: Integer read FSleepTime write FSleepTime;
property OnSearchURL: TSearchURLEvent read FSearchURLEvent write FSearchURLEvent;
property OnURLError: TURLErrorEvent read FURLErrorEvent write FURLErrorEvent;
end;
var
CurrentURL: string;
ErrorMsg: string;
CriticalSection: TRTLCriticalSection;
implementation
{ TWebRobot }
constructor TWebRobot.Create;
begin
inherited Create(True);
FSleepTime:= 3000;
FHTTP:= TIdHTTP.Create(nil);
FHTTP.Request.UserAgent:= 'DevBot';
FHTTP.ReadTimeout:= 10000;
FreeOnTerminate:= True;
// Priority := tpIdle;
InitializeCriticalSection(CriticalSection);
end;
destructor TWebRobot.Destroy;
begin
FHTTP.Free;
DeleteCriticalSection(CriticalSection);
inherited;
end;
procedure TWebRobot.ErrorCallback;
begin
if Assigned(FURLErrorEvent) then
FURLErrorEvent(self, CurrentURL, ErrorMsg);
end;
function TWebRobot.GetURLText(const URL: string): string;
begin
Result:= '';
try
UpdateURL(URL);
Synchronize(URLCallback);
Result:= FHTTP.Get(URL);
except
on E: Exception do
begin
UpdateError(E.Message);
Synchronize(ErrorCallback);
end;
end;
end;
procedure TWebRobot.UpdateError(const Msg: string);
begin
EnterCriticalSection(CriticalSection);
try
ErrorMsg:= Msg;
finally
LeaveCriticalSection(CriticalSection);
end;
end;
procedure TWebRobot.UpdateURL(const URL: string);
begin
EnterCriticalSection(CriticalSection);
try
CurrentURL:= URL;
finally
LeaveCriticalSection(CriticalSection);
end;
end;
procedure TWebRobot.URLCallback;
begin
if Assigned(FSearchURLEvent) then
FSearchURLEvent(self, CurrentURL);
end;
end.
|