我想对一个网址进行判断,如果是网页那么获得其文本,如果是文件那么进行下载,但是我无法判断是文件(比如zip,rar)还是网页(asp,jsp,html),更可恶的是网页重定向后根本无法从网址中的文件扩展名中判断出是不是文件,比如如下网址:
URL http://www.zg51.com/SoftDown.asp?ID=705
重定向后指向一个PodDot.exe文件,并不是指向一个网页,如果用TIDHttp组件的Head方法判断的话太慢,有没有更好更快的方法呢?---------------测试网址----------
http://www.violinbbs.com/bbs/viewFile.asp?Boardid=41&ID=1485  指向一个mp3
URL http://download.pchome.net/php/tdownload2.php?sid=849&url=/development/delphi/vp21b243.zip&svr=2&typ=0 指向一个zip

解决方案 »

  1.   

    我不会,你看这代码有用没:
    uses UrlMon;function DownloadFile(SourceFile, DestFile: string): Boolean;
    begin
      try
        Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
      except
        Result := False;
      end; 
    end; procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j: Integer;
      Source,Dest,ext: OleVariant;
    begin
      WebBrowser1.Navigate('http://www.xxx.com');
      while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
        Application.ProcessMessages;  if WebBrowser1.OleObject.Document.all.tags('A').Length = 0 then Exit;
      Memo1.Clear;  for i := 0 to WebBrowser1.OleObject.Document.all.tags('A').Length - 1 do
      begin
        Source := WebBrowser1.OleObject.Document.all.tags('A').Item(i);
        j := LastDelimiter('.', Source);
        ext := UpperCase(Copy(Source, j+1, Length(Source)));    if (ext = 'RAR') or (ext = 'ZIP') then
        begin
          Memo1.Lines.Add(Source.innerText + ': ' + Source.href);
          Dest := ExtractFilePath(ParamStr(0)) + Source.innerText;
          DownloadFile(Source, Dest);
        end;
      end;
    end;説明:用「TWebBrowser」控件、下载网页中<a href="...">
      

  2.   

    帮你用WPE截了下数据包
    打开链接时,发送:
    GET http://download.pchome.net/php/tdownload2.php?sid=849&url=/development/delphi/vp21b243.zip&svr=2&typ=0 HTTP/1.0..Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*..Accept-Language: zh-cn..User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; .NET CLR 1.0.3705)..Host: download.pchome.net..Proxy-Connection: Keep-Alive....然后接收到如下数据:
    HTTP/1.1 302 Found..Date: Thu, 18 Nov 2004 00:33:11 GMT..Server: Apache..X-Powered-By: PHP/4.3.8..Location: http://dlb.pchome.net/development/delphi/vp21b243.zip..Content-Length: 0..Connection: close..Content-Type: text/html; charset=gb2312....
    从Location: http://dlb.pchome.net/development/delphi/vp21b243.zip这句我们可以知道重定向的URL.至于http://www.violinbbs.com/bbs/viewFile.asp?Boardid=41&ID=1485这个URL,要登录,测试不了.