求DELPHI获取网页源码的一段代码

2025-03-07 14:38:56
推荐回答(2个)
回答1:

以下内容是一个单元,保存为MyIdHTTP.pas后,由主程序uses这个单元,调用GetStrOfURL即可。

unit MyIdHTTP;

interface

uses IdHTTP, SysUtils;

const
CN_Count_Retry = 5; // 失败后重试次数
CN_SleepBetweenRetry = 3000; // 重试前延迟毫秒数

function GetStrOfURL(
const sURL : string;
var sContent : string;
nRetryCount : Integer = CN_Count_Retry;
nSleepMS : Integer = CN_SleepBetweenRetry
) : Boolean;

implementation

var
AIdHTTP : TIdHTTP;

function GetStrOfURL(
const sURL : string;
var sContent : string;
nRetryCount : Integer = CN_Count_Retry;
nSleepMS : Integer = CN_SleepBetweenRetry
) : Boolean;
begin
Result := False;
while nRetryCount > 0 do
try
sContent := AIdHTTP.Get( sURL );
Result := True;
Break;
except
Dec( nRetryCount );
Sleep( nSleepMS );
end;
end;

initialization
AIdHTTP := TIdHTTP.Create( nil );

finalization
FreeAndNil( AIdHTTP );

end.

回答2:

放入控件中的IWLayoutMgrHTML1双击可以看到网页源码