致力于测试团队建设和自动化测试开发,欢迎有兴趣者一起研究讨论

使用XMLHttp和ADODB.Stream取得远程文件并保存

上一篇 / 下一篇  2008-07-23 12:15:56 / 个人分类:Automation

-~0q;Gu4qs+K;k0'取得远程文件并保存到本地 
5zW+i#w1M0Function GetRemoteFiels(RemotePath, LocalPath, FileName) 51Testing软件测试网 p#RxA;C/z&D2o"J
Dim strBody 
vW&`6i{0Dim FilePath 51Testing软件测试网+p*um;FU? P b
51Testing软件测试网;SO$^0oP-`~
  On Error Resume Next 
8OW)D^1U1AWoej051Testing软件测试网H,?8^dz-D;M \1vB3p^
  '取得流 
p:u;w"B iO"X0strBody = GetBody(RemotePath) 
q-[:LO1f H'I(Sx0'取得保存的文件名 
$E5s(\ g"H"z I$nT P0if Right(LocalPath, 1) <> "\" then LocalPath = LocalPath & "\" 
BM~1w4?V0FilePath = LocalPath & GetFileName(RemotePath, FileName) 51Testing软件测试网1rK0^3AT
'保存文件 51Testing软件测试网3W.L` K@#N
if SaveToFile(strBody, FilePath) = true and err.Number = 0 then 
/J#zCVo1k']F|:x*H&E0   GetRemoteFiles = true 
)s'QX1i(D/^6i0else 
8K&q~!G;H/WC0   GetRemoteFiles = false 
8^-m!Hh0l!l @0end if 
0r$W{ X&xu/~(@051Testing软件测试网[0T B&Orx*O3j"G
End Function 
bx k#Vu5n!] i051Testing软件测试网:Wj-I4KC0ft\u
'远程获取内容 51Testing软件测试网7QS q\k!F#qih
Function GetBody(url) 
#lh+kHc/kUbF0Dim Retrieval 
/J9GkN&Z2P!_9}A ^K-Jw0  '建立XMLHTTP对象 51Testing软件测试网 N KX)qj+re
  Set Retrieval = createObject("Microsoft.XMLHTTP") 
RI%Q,gG;K.J!zAm0  With Retrieval 
2T7Df;H4V(c3Q0    .Open "Get", url, False, "", "" 51Testing软件测试网/N,zq~#|%Db)JU
    .Send 
W4U!]7bv(] Q g0    GetBody = .ResponseBody 
z t6{1H:vp },{ D0  End With 
0X?LQ'lt0  Set Retrieval = Nothing 
o+d _4rD3y ?}1R'QDp b0End Function 
7L*Q-OmA-u5Q051Testing软件测试网 E?1^]\$af
'重组文件名 
AXqm;v(xQ0Function GetFileName(RemotePath, FileName) 
fb'K{7D(c2Nq0Dim arrTmp 
k%r}Y6]Q,\_7qz0Dim strFileExt 51Testing软件测试网Kf aw7|/U/V;@o'u
  arrTmp = Split(RemotePath, ".") 
D-ls1Wg,zPA+c+y0strFileExt = arrTmp(UBound(arrTmp)) 51Testing软件测试网5U&N3{XZQh
  GetFileName = FileName & "." & strFileExt 
}0e+F7p(CX;yc0End Function 51Testing软件测试网hZi7fTt
51Testing软件测试网xYs9GP"^\^
'将流内容保存为文件 51Testing软件测试网l f(KI0|2_ A/u
Function SaveToFile(Stream, FilePath) 51Testing软件测试网&X&r m @(G,{pw
Dim objStream 51Testing软件测试网yRBb[

6t S%yzl.u~X0  On Error Resume Next 
D/^h:NO1m0
N.O.u r}:``;JK0  '建立ADODB.Stream对象,必须要ADO 2.5以上版本 51Testing软件测试网fv&q$K#G Dga
  Set objStream = createObject("ADODB.Stream") 51Testing软件测试网 d }.B+vA mR9S
  objStream.Type = 1 '以二进制模式打开 
i^Mk Y0  objStream.Open 
UTrb`4W8E%U!u0  objstream.write Stream 
Z-Oub a0  objstream.SaveToFile FilePath, 2 51Testing软件测试网'WFiI Q.q
  objstream.Close() 51Testing软件测试网)az^yv
  '关闭对象,释放资源 
z9A?$VkhN0  Set objstream = Nothing 
&X@6Np0L/HUx]051Testing软件测试网i:f@?*DH
if err.Number <> 0 then 
A,@:v1io0   SaveToFile = false 
CGDzrP-}uIj,o0else 
f$p kI0u/Y:R\7n*ja w0   SaveToFile = true 
+e%Rd@ HX T2s M0end if 51Testing软件测试网O(JV$zq})v)sn
End Function 
'O^$l'D(hw#_051Testing软件测试网P[ ?ri,E]J


TAG: 自动化测试

 

评分:0

我来说两句

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 91076
  • 日志数: 79
  • 图片数: 1
  • 建立时间: 2008-05-18
  • 更新时间: 2009-06-04

RSS订阅

Open Toolbar