获取网页源代码的最简单办法

来源:互联网 发布:linux 测试apache 编辑:程序博客网 时间:2024/04/28 04:41
获取网页源代码的最简单办法,就是利用 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;



使用时,直接把收到的源代码显示出来:


Memo1.Text := GetWebPage('http://www.tommstudio.com/');

原创粉丝点击