谁能全面解析一下 DELPHI中的 UrlMon.pas(50分)

  • 主题发起人 主题发起人 noko1981
  • 开始时间 开始时间
N

noko1981

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能全面解析一下 DELPHI中的 UrlMon.pas
 
温柔一刀 (2000-06-18 19:02:00) &nbsp;<br>urlmon实现了urlmon.dll的com接口,<br>可以用来让你编的ActiveForm能够控制它的宿主浏览器,例如:<br><br>procedure TMyActiveForm.btRedirectClick(Sender: TObject);<br>begin<br>HLinkNavigateString(IUnknown(VCLComObject),'http://www.myserver.com');<br>end;<br><br>可以实现redirect到另一个地址,其中HLinkNavigateString是urlmon中的函数。 <br> <br><br><br><br>天真 (2002-05-09 17:46:00) &nbsp;<br>关于把网页,以及与网页相关的都保存到硬盘中!<br>这个答案如下:例子的效果把www.sina.com.cn的网页保存下来!:)<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,nmhttp,urlmon,<br> &nbsp;StdCtrls, ExtCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;Memo2: TMemo;<br> &nbsp; &nbsp;Splitter1: TSplitter;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;procedure savedata(url,memo:string);<br> &nbsp; &nbsp;function &nbsp;dealwith(url,baseurl:string):string; //处理网页路径<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.savedata(url,memo:string);<br>function DownloadFile(Source, Dest: string): Boolean;<br>begin<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=False;<br> &nbsp; &nbsp;end;<br>end;<br><br>function getfilename(s:string):string;<br>var i:integer;<br>begin<br> &nbsp; &nbsp;for i:=length(s) downto &nbsp;1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if (s='/')or(s='/') then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;result:=copy(s,i+1,length(s)-i);<br>end;<br><br>var &nbsp; &nbsp; i:integer;<br> &nbsp; &nbsp;imagelist:tstringlist;<br> &nbsp; &nbsp;bmplist:tstringlist;<br> &nbsp; &nbsp;tempmemo:string;<br> &nbsp; &nbsp;beginpos,endpos:integer;<br> &nbsp; &nbsp;path:string;<br> &nbsp; &nbsp;s:string;<br> &nbsp; &nbsp;memorystream:tmemorystream;<br>begin<br><br> &nbsp; &nbsp;bmplist:=tstringlist.Create ;<br> &nbsp; &nbsp;bmplist.Add('.bmp');<br> &nbsp; &nbsp;bmplist.Add('.jpg');<br> &nbsp; &nbsp;bmplist.Add('.swf');<br> &nbsp; &nbsp;bmplist.Add('.gif');<br><br> &nbsp; &nbsp;path:='d:/download/';<br> &nbsp; &nbsp;tempmemo:=memo;<br> &nbsp; &nbsp;imagelist:=tstringlist.create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp;while pos('src=',memo)&gt;0 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;beginpos:=pos('src=',memo)+4;<br> &nbsp; &nbsp; &nbsp; &nbsp;while (memo[beginpos]=' ') or (memo[beginpos]='&quot;') do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(beginpos);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;endpos:=beginpos+1;<br> &nbsp; &nbsp; &nbsp; &nbsp;while (memo[endpos]&lt;&gt;' ') and (memo[endpos]&lt;&gt;'&quot;')and (memo[endpos]&lt;&gt;'&gt;')do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(endpos);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br>// &nbsp; &nbsp; &nbsp; &nbsp;s:=copy(memo,beginpos,endpos-beginpos);<br> &nbsp; &nbsp; &nbsp; &nbsp;s:=dealwith(copy(memo,beginpos,endpos-beginpos),'www.sina.com.cn');<br>// &nbsp; &nbsp; &nbsp; &nbsp;showmessage(s);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (imagelist.indexof(s)=-1)or(imagelist=nil) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;imagelist.Add(s);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DownloadFile(s,'d:/download/'+getfilename(s));<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;delete(memo,1,endpos);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp;showmessage(inttostr(beginpos)+','+inttostr(endpos));<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;showmessage(inttostr(imagelist.count));<br> &nbsp; &nbsp;for i :=0 to imagelist.count-1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;memo1.text:=stringreplace(memo1.text,imagelist.strings,getfilename(imagelist.strings),[rfReplaceAll]);<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;memorystream:=tmemorystream.Create ;<br> &nbsp; &nbsp;memorystream.Position :=0;<br> &nbsp; &nbsp;imagelist.Free;<br> &nbsp; &nbsp;bmplist.Free ;<br> &nbsp; &nbsp;memorystream.Free ;<br> &nbsp; &nbsp;memo1.Lines.SaveToFile('d:/download/index.htm');<br> &nbsp; &nbsp;showmessage('ok');<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var s:string;<br> &nbsp; &nbsp;nmhttp:tnmhttp;<br>begin<br> &nbsp; &nbsp;//建立nmhttp<br> &nbsp; &nbsp;nmhttp:=tnmhttp.Create(nil);<br> &nbsp; &nbsp;NMHTTP.InputFileMode := FALSE;<br> &nbsp; &nbsp;NMHTTP.OutputFileMode := FALSE;<br>// &nbsp; &nbsp;NMHTTP.ReportLevel := Status_Basic;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;nmhttp.Get('www.sina.com.cn');<br> &nbsp; &nbsp; &nbsp; &nbsp;s:=nmhttp.body;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;memo1.text:=s;<br><br> &nbsp; &nbsp;savedata('sss',s);<br><br>end;<br><br>function &nbsp;Tform1.dealwith(url,baseurl:string):string; //处理网页路径<br>var i:integer;<br> &nbsp; &nbsp;beginpos:integer;<br>begin<br> &nbsp; &nbsp;URL:=stringreplace(url,'&quot;','',[rfReplaceAll]);<br> &nbsp; &nbsp;if url[1]+url[2]+url[3]+url[4]+url[5]+url[6]+url[7]='http://' then<br> &nbsp; &nbsp; &nbsp; &nbsp;dealwith:=url;<br><br> &nbsp; &nbsp;URL:=stringreplace(url,'/','/',[rfReplaceAll]);<br><br> &nbsp; &nbsp;if pos(' ',url)&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;url:=copy(url,1, pos(' ',url)-1);<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;for i:=length(baseurl) downto 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if baseurl='/' then<br> &nbsp; &nbsp; &nbsp; &nbsp;BEGIN<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;END;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;IF I&lt;&gt;1 THEN<br> &nbsp; &nbsp; &nbsp; &nbsp;baseurl:=copy(baseurl,1,i)<br> &nbsp; &nbsp;ELSE<br> &nbsp; &nbsp; &nbsp; &nbsp;BASEURL:=BASEURL+'/';<br><br> &nbsp; &nbsp;if ((pos('.',url)&lt;&gt;0)and(pos('./',url)=0)and(url[1]&lt;&gt;'/')) or (pos('www.',url)&lt;&gt;0) then<br> &nbsp; &nbsp; &nbsp; &nbsp;dealwith:=url;<br><br> &nbsp; &nbsp;if (pos('../',url)=0)AND (POS('./',URL)=0) then //没有指定目录//??要改改<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if url[1]='/' then<br> &nbsp; &nbsp; &nbsp; &nbsp;url:=copy(url,2,length(url)-1);<br> &nbsp; &nbsp; &nbsp; &nbsp;dealwith:=baseurl+url;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;i:=length(baseurl)-1;<br> &nbsp; &nbsp; &nbsp; &nbsp;while pos('../',url)&lt;&gt;0 do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;beginpos:=pos('../',url);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(url,1,beginpos+2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while i&gt;=1 do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if baseurl='/' then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dec(i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dec(i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;dealwith:=copy(baseurl,1,i)+'/'+url;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;dealwith:=url;<br>end;<br><br>end.<br><br> <br>------------------<br>随便找了一下,只有这点资料。
 
笑傲江湖1976 (2003-12-08 16:28:00) &nbsp;<br>Uses UrlMon;<br> &nbsp; &nbsp;SourceFile:='http://192.168.30.10/webpr/RptSals/Sals.xls';<br> &nbsp; &nbsp;DestFile:='c:/Sals.xls';<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;UrlDownLoadToFile(IUnknown(VclComObject),SourceFIle,DestFile,0,nil);<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp;end;
 
楼主,建议你在大富翁里查一下。有很多资料,基本上可以让你知道这个单元的功能。
 
主要想知道如何得到下载进度
 
后退
顶部