试试!
proceduredo
wnloadFile(strHost, strRemoteFileName, strLocalFileName: string;
ClientSocket: TClientSocket);
var
intReturnCode: Integer;
s: string;
szBuffer: array[0..128] of Char;
FileOut: TFileStream;
begin
if strRemoteFileName[1] <> '/' then
strRemoteFileName := '/' + strRemoteFileName;
FileOut := TFileStream.Create(strLocalFileName, fmCreate);
try
with ClientSocketdo
begin
Host := strHost;
ClientType := ctBlocking;
Port := 80;
try
Open;
s := 'GET ' + strRemoteFileName + ' HTTP/1.0'#13#10 +
'Host: ' + strHost + #13#10#13#10;
intReturnCode := Socket.SendBuf(Pointer(s)^, Length(s));
if intReturnCode > 0 then
begin
while (intReturnCode > 0)do
begin
FillChar(szBuffer, SizeOf(szBuffer), 0);
intReturnCode := Socket.ReceiveBuf(szBuffer, SizeOf(szBuffer));
if intReturnCode > 0 then
FileOut.Write(szBuffer, intReturnCode);
end
end
else
MessageDlg('No answer from server', mtError, [mbOk], 0);
Close;
except
MessageDlg('No connection', mtError, [mbOk], 0);
end;
end;
finally
FileOut.Free
end;
end;
调用例子:
procedure TForm1.Button1Click(Sender: TObject);
begin
do
wnloadFile('www.scalabium.com', '/forums.htm', 'd:/forums.htm', ClientSocket1);
end;