delphi获取网页源代码

方法一、利用 WinInet 单元中的函数:

uses WinInet;
function GetWebPage(const Url: string):string;
var
  Session,
  HttpFile:HINTERNET;
  szSizeBuffer:Pointer;
  dwLengthSizeBuffer:DWord;
  dwReserved:DWord;
  dwFileSize:DWord;
  dwBytesRead:DWord;
  Contents:PChar;
begin
  Session:=InternetOpen(’’,0,niL,niL,0);
  HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,0,0);
  dwLengthSizeBuffer:=1024;
  HttpQueryInfo(HttpFile,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
  GetMem(Contents,dwFileSize);
  InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
  InternetCloseHandle(HttpFile);
  InternetCloseHandle(Session);
  Result:=StrPas(Contents);
  FreeMem(Contents);
end;

方法二、通过Indy控件

添加一个IdHttp空间,然后

var
sl:Tstringlist;
begin
sl:=Tstringlist.Create;
try
sl.Text:=idhttp1.Get('http://www.programbbs.com');
memo1.Text:=sl.Text;
finally
sl.Free;
end;
end;

posted @ 2012-08-16 18:09  逍遥豆子  阅读(349)  评论(0)    收藏  举报