其中一段给点提示 //发送get请求,以得到实际的文件数据 FClientSocket.Active:=false; FClientSocket.Active:=true; url1:=''; url1:=url1+'GET /'+sSvrFileName+' HTTP/1.1'+#13#10; url1:=url1+'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*'+#13#10; url1:=url1+'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)'+#13#10; //接受数据的范围,可选 if endBytePos<=0 then begin url1:=url1+'RANGE: bytes='+inttostr(FPriorPos)+'-'+#13#10; end else begin url1:=url1+'RANGE: bytes='+inttostr(FPriorPos)+'-'+IntToStr(endBytePos)+#13#10; end; //下面这句必须要有 url1:=url1+'Host: '+sSvrHost+#13#10; url1:=url1+#13#10; FClientSocket.Socket.SendText(url1); //头信息处理 while FClientSocket.Active=true do begin if FStop=true then break; cmd1:=SocketReadLine(FClientSocket.Socket);//取的每一行返回的信息 if pos(lowercase('Content-Range:'),lowercase(cmd1))=1 then begin//判断是否有找到在确结果,是否可接收 ExistsFile:=true; end; if pos(lowercase('Content-Length: '),lowercase(cmd1))=1 then begin//取文件接收的长度 value1:=copy(cmd1,length('Content-Length: ')+1,length(cmd1)); reclen1:=strtoint(trim(value1)); end; if cmd1=#13#10 then break; end;//end while
基于上面的原理,写一个ACTIVEX组件,挂一在网页上就OK了,诺顿就是这样做的
现成的API 包含在UrlMon单元里 UrlDownLoadToFile,最后个参数可以回调一个过程 function OnProgress(ulProgress, ulProgressMax,ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult; begin //Caption:='已下载:' + IntToStr(ulProgress) + '字节'+';总共:' + IntToStr(ulProgressMax)+'字节'; end;
FClientSocket.Active:=false;
FClientSocket.Active:=true;
url1:='';
url1:=url1+'GET /'+sSvrFileName+' HTTP/1.1'+#13#10;
url1:=url1+'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*'+#13#10;
url1:=url1+'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)'+#13#10;
//接受数据的范围,可选
if endBytePos<=0 then
begin
url1:=url1+'RANGE: bytes='+inttostr(FPriorPos)+'-'+#13#10;
end
else
begin
url1:=url1+'RANGE: bytes='+inttostr(FPriorPos)+'-'+IntToStr(endBytePos)+#13#10;
end;
//下面这句必须要有
url1:=url1+'Host: '+sSvrHost+#13#10;
url1:=url1+#13#10;
FClientSocket.Socket.SendText(url1);
//头信息处理
while FClientSocket.Active=true do
begin
if FStop=true then
break;
cmd1:=SocketReadLine(FClientSocket.Socket);//取的每一行返回的信息
if pos(lowercase('Content-Range:'),lowercase(cmd1))=1 then
begin//判断是否有找到在确结果,是否可接收
ExistsFile:=true;
end;
if pos(lowercase('Content-Length: '),lowercase(cmd1))=1 then
begin//取文件接收的长度
value1:=copy(cmd1,length('Content-Length: ')+1,length(cmd1));
reclen1:=strtoint(trim(value1));
end;
if cmd1=#13#10 then
break;
end;//end while
包含在UrlMon单元里
UrlDownLoadToFile,最后个参数可以回调一个过程
function OnProgress(ulProgress, ulProgressMax,ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;
begin
//Caption:='已下载:' + IntToStr(ulProgress) + '字节'+';总共:' + IntToStr(ulProgressMax)+'字节';
end;