如何写远程下载程序???(50分)

  • 主题发起人 主题发起人 rickma
  • 开始时间 开始时间
R

rickma

Unregistered / Unconfirmed
GUEST, unregistred user!
用delphi写一个可以定时下载远程服务器中文件的程序,文件类型不限[?]
 
下载代码如下,定时就自己加一个Time事件吧!

procedure DownloadFile(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 ClientSocket do
begin
Host := strHost;
ClientType := ctBlocking;
Port := 80;

try
Open;
{send query}
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
{receive the answer}
{ iterate until no more data }
while (intReturnCode > 0) do
begin
{ clear buffer before each iteration }
FillChar(szBuffer, SizeOf(szBuffer), 0);

{ try to receive some data }
intReturnCode := Socket.ReceiveBuf(szBuffer, SizeOf(szBuffer));

{ if received a some data, then add this data to the result string }
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
DownloadFile('www.scalabium.com', '/forums.htm', 'd:/forums.htm', ClientSocket1);
end;
 
YB_unique,我发现如果文件不是htm类型的话,只是创建一个大小与下载文件相同的文件,然后
就下载少量的文件块,就结束了,测试环境delphi6 + iis
 
后退
顶部