|
컴파일은 해보지 않았습니다. 오타가 있을수도 있겠군요..
참고하시기 바랍니다.
=======================================================================================
//
// header file ...
//
typedef void __fastcall (__closure *TSearchURLEvent)(TObject *Sender, const String URL);
typedef void __fastcall (__closure *TURLErrorEvent)(TObject *Sender, const String URL, const String Msg);
class TWebRobot : public TThread
{
private:
TIdHTTP * FHTTP;
TURLErrorEvent FURLErrorEvent;
int FSleepTime;
TSearchURLEvent FSearchURLEvent;
void __fastcall UpdateURL(const String URL);
void __fastcall UpdateError(const String Msg);
protected:
void __fastcall URLCallback();
void __fastcall ErrorCallback();
public:
__fastcall TWebRobot();
virtual __fastcall ~TWebRobot();
String __fastcall GetURLText(const String URL);
public:
__property int SleepTime = {read = FSleepTime, write = FSleepTime};
__property TSearchURLEvent OnSearchURL = {read = FOnSearchURL, write = FOnSearchURL};
__property TURLErrorEvent OnURLError = {read = FOnURLError, write = FOnURLError};
};
=======================================================================================
extern String CurrentURL;
extern String ErrorMsg;
extern RTL_CRITICAL_SECTION CriticalSection;
//
// cpp file ....
//
String CurrentURL = "";
String ErrorMsg = "";
RTL_CRITICAL_SECTION CriticalSection = {0, };
//----------------------------------------------------------------------------
__fastcall TWebRobot::TWebRobot() : TThread(true)
{
FSleepTime = 3000;
FHTTP = new TIdHTTP(NULL);
FHTTP->Request->UserAgent = "DevBot";
FHTTP->ReadTimeout = 10000;
FreeOnTerminate = true;
::InitializeCriticalSection(&CriticalSection);
}
//----------------------------------------------------------------------------
__fastcall TWebRobot::~TWebRobot()
{
FHTTP->Free();
::DeleteCriticalSection(&CriticalSection);
}
//----------------------------------------------------------------------------
void __fastcall TWebRobot::ErrorCallback()
{
if(FURLErrorEvent)
FURLErrorEvent(this, CurrentURL, ErrorMsg);
}
//----------------------------------------------------------------------------
String __fastcall TWebRobot::GetURLText(const String URL)
{
try
{
UpdateURL(URL);
Synchronize(URLCallback);
return FHTTP->Get(URL);
}
catch(Exception &E)
{
UpdateError(E.Message);
Synchronize(ErrorCallback);
return "";
}
}
//----------------------------------------------------------------------------
void __fastcall TWebRobot::UpdateError(const String Msg)
{
::EnterCriticalSection(&CriticalSection);
try
{
ErrorMsg = Msg;
}
__finally
{
::LeaveCriticalSection(&CriticalSection);
}
}
//----------------------------------------------------------------------------
void __fastcall TWebRobot::UpdateURL(const String URL)
{
::EnterCriticalSection(&CriticalSection);
try
{
CurrentURL = URL;
}
__finally
{
::LeaveCriticalSection(&CriticalSection);
}
}
//----------------------------------------------------------------------------
void __fastcall TWebRobot::URLCallback()
{
if(FSearchURLEvent)
FSearchURLEvent(this, CurrentURL);
}
//----------------------------------------------------------------------------
|