获取网页内容的问题(100)

  • 主题发起人 主题发起人 littlecat2008
  • 开始时间 开始时间
L

littlecat2008

Unregistered / Unconfirmed
GUEST, unregistred user!
来自:小雨哥, 时间:2003-2-22 4:49:00, ID:1637595 获取网页源代码的最简单办法,就是利用 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.delphibbs.com/delphibbs/listq.asp'); 网上找到的这段代码,在普通的form程序里可以获取到网页信息,可是放到console application程序里就取不到数据了,放在主程序和子线程里都取不到信息,为什么?[:(!]
 
[:D]乍一用感觉还行,再用发现有时候数据能取全,有时候数据都取不全~谁用过delphi2009?delphi2009的idhttp控件还有问题吗?
 
你建立消息循环了没? ....如果没建立 InternetReadFile读一次数据后程序就退出了....program Project1;uses Windows, messages, Classes, 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; {$R *.res}var Msg: tMsg; strs:tstringlist;begin strs:=tstringlist.create; strs.text:=GetWebPage('http://www.163.com'); strs.SaveToFile('d:/testweb.html'); while GetMessage(Msg, 0, 0, 0) do {怎么退出你自己处理.....} begin TranslateMessage(Msg); DispatchMessage(Msg); end; strs.free;end.
 
[^]打算用VC取了
 
加个count>0的判断,累加取
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部