E
endy
Unregistered / Unconfirmed
GUEST, unregistred user!
用以下代码可以下载文件:
procedure tform1.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;
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);
caption := inttostr(intReturnCode);
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;
但下载文件会多出一段服务器信息之内的东西,比如:
原txt文件:
ver=1.1
http=192.168.1.181
path=/test/
fileCount=3
filever1=1.0
filename1=wordpad.exe
filepppath1=wordpad.exe
filever2=1.0
filename2=test
filepppath2=test
filever3=1.0
filename3=tt1
filepppath3=tt1
将变成:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 01 Nov 2001 07:53:07 GMT
Content-Type: text/plain
Accept-Ranges: bytes
Last-Modified: Wed, 31 Oct 2001 09:46:56 GMT
ETag: "02827faf061c11:883"
Content-Length: 214
ver=1.1
http=192.168.1.181
path=/test/
fileCount=3
filever1=1.0
filename1=wordpad.exe
filepppath1=wordpad.exe
filever2=1.0
filename2=test
filepppath2=test
filever3=1.0
filename3=tt1
filepppath3=tt1
我特别需要下载exe等可执行文件,如何去掉多出的服务器信息部分?
曾试过在buffer找两个回车的方法,但不知什么原因无法成功。
实在束手无策,万忘指点一二,如有其他方法实现相同功能也可。
如能帮助小弟,请尽可能的告知详细实现方法,我真的是没办法了啊。。。。。。。。
procedure tform1.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;
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);
caption := inttostr(intReturnCode);
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;
但下载文件会多出一段服务器信息之内的东西,比如:
原txt文件:
ver=1.1
http=192.168.1.181
path=/test/
fileCount=3
filever1=1.0
filename1=wordpad.exe
filepppath1=wordpad.exe
filever2=1.0
filename2=test
filepppath2=test
filever3=1.0
filename3=tt1
filepppath3=tt1
将变成:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 01 Nov 2001 07:53:07 GMT
Content-Type: text/plain
Accept-Ranges: bytes
Last-Modified: Wed, 31 Oct 2001 09:46:56 GMT
ETag: "02827faf061c11:883"
Content-Length: 214
ver=1.1
http=192.168.1.181
path=/test/
fileCount=3
filever1=1.0
filename1=wordpad.exe
filepppath1=wordpad.exe
filever2=1.0
filename2=test
filepppath2=test
filever3=1.0
filename3=tt1
filepppath3=tt1
我特别需要下载exe等可执行文件,如何去掉多出的服务器信息部分?
曾试过在buffer找两个回车的方法,但不知什么原因无法成功。
实在束手无策,万忘指点一二,如有其他方法实现相同功能也可。
如能帮助小弟,请尽可能的告知详细实现方法,我真的是没办法了啊。。。。。。。。