没有窗体的程序怎样获得网页内容 ( 积分: 100 )

  • 主题发起人 主题发起人 starsoul
  • 开始时间 开始时间
S

starsoul

Unregistered / Unconfirmed
GUEST, unregistred user!
最近在写一个没有窗体的程序,不知道怎样才能获得网页上的内容?网上找了一下很多都是用IdHTTP控件,但我的程序只有一个dpr,为了事使程序能够小一点,所以没有使用FORMS.使用winsock可以获得网页内容,但没找到详细的资料.
 
用synapse的httpsend
 
刚才在网上找到了不用控件的代码:
(要use 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;
 
uses UrlMon;

var
fName: PChar;

function GetUrl(url: PChar): PChar;
var
cf: array [0..259] of char;
begin
if URLDownloadToCacheFile(nil, url, cf, 260, 0, nil)=S_OK then
result := cf
else
result := nil;
end;

begin
fName := GetUrl('http://www.delphibbs.com/delphibbs/dispq.asp?lid=3707607');
if fName <> nil then
begin
//可以对fName文件进行操作。也可以创建TStringList进行LoadFromFile后对Text进行操作。
end;
else
Writeln('Error!');
Readln;
end.
 
program GetUrlSource;

{$APPTYPE CONSOLE}

uses
SysUtils,
Classes,
IdHTTP;

var
_HTTP : TIDHTTP;
FileStream : TMemoryStream;

begin
_HTTP := TIdHTTP.Create(nil);
FileStream := TMemoryStream.Create;
if (ParamStr(1)<>'') and (ParamStr(2)<>'') then
begin
try
_HTTP.Get(ParamStr(1),FileStream);
FileStream.SaveToFile(ParamStr(2));
except
Writeln('Error...');
end;
end;
end.
 
program GetUrlSource;

{$APPTYPE CONSOLE}

uses
SysUtils,
Classes,
IdHTTP;

var
_HTTP : TIDHTTP;
FileStream : TMemoryStream;

begin
_HTTP := TIdHTTP.Create(nil);
FileStream := TMemoryStream.Create;
if (ParamStr(1)<>'') and (ParamStr(2)<>'') then
begin
try
_HTTP.Get(ParamStr(1),FileStream);
FileStream.SaveToFile(ParamStr(2));
Writeln('Save success! FilePath: '+ParamStr(2));
except
Writeln('Error...');
end;
end
else
begin
Writeln('------------------------------------------------------------');
Writeln(' #GetUrlSource');
Writeln(' #Author:Dreams');
Writeln(' #Example:');
Writeln(' C:/>GetUrlSource.exe http://www.baidu.com C:/Baidu.html');
Writeln('------------------------------------------------------------');
end;
FileStream.Free;
_HTTP.Free;
end.
 
用纯socket api+http协议就行


...socet init

...connect to host

s := '';
s := s + 'GET /' + 页面地址+ ' HTTP/1.1' + #13#10;
s := s + 'Accept: */*' + #13#10;
s := s + 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)' + #13#10;
s := s + 'RANGE: bytes=' + IntToStr(StartPos) + '-' + #13#10; //文件开始下载的位置
s := s + 'Host: ' + 主机域名()+ #13#10;
s := s + #13#10;

send(d.m_sock, pchar(s)^, length(s), 0);

发送之后,再接收数据
while True do
begin
n := recv(d.m_sock, c, 1, 0);
if n<=0 then break;
....
end;//
 
http://www.51zhan.com 最好的网址站
http://www.51zhan.com 最好的网址站
http://www.51zhan.com 最好的网址站
 
加我的QQ923557
 
接受答案了.
 
http://www.51zhan.com 最好的网址站
http://www.51zhan.com 最好的网址站
http://www.51zhan.com 最好的网址站
 
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;
还可以进行地址的转向.
 
接受答案了.
 
后退
顶部