随笔-13  评论-5  文章-3  trackbacks-0
作者:blue1019
来源:http://spaces.msn.com/members/blue1019/Blog/cns!1pvHzJ2FFuxbZKKCb2_fpGcA!305.entry

10月10日
获取远程服务器数据(asp,adodb.stream,保存远程图片,XMLHTTP)
本文章对使用xmlhttp下载其他服务器信息的技术做了进一步的研究,如不熟悉请先阅读
 
小偷是怎样炼成的
 
关于XMLHTTP的细节可以参考
 
关于adodb.stream的细节可以参考
 
 
下面重新注释了BytesToBstr函数,它的作用是把由xmlhttp.responsebody返回的字节数组转换成文本字符串,如果数据要用做屏幕输入则必须转换否则汉字会出现乱码.
 
 Function BytesToBstr(body)
  'Cset:GB2312 UTF-8
  dim objstream
  set objstream = Server.CreateObject("adodb.stream")
  with objstream
   .Type = 1 '设置返回数据类型为二进制
   .Mode = 3 '打开模式为读写
   .Open    
   .Write body  '将指定的数据装入对像中 body为内容
   .Position = 0 '指定对像内数据的当前指针
   .Type = 2  '设置返回数据类型为文本
   .Charset = Cset  '设定字符集类型
   BytesToBstr = .ReadText '取对象内的文本
   .Close
  end with
  set objstream = nothing
 End Function
 
 
获取其他服务器的数据和获取文本是一个道理,通过图片的url获取图片的数据处理,只是保存的时候不需要把它转换成字符串了。这里利用adodb.stream的SaveToFile方法将其保存在本地服务器上。
 
 Public function saveimage(tofile)
  dim objStream,imgs
  imgs=getBody(sUrl)'取得图片的具休内容的过程
  Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO 2.5以上版本
  with objStream
   .Type =1'以二进制模式打开
   .Open
   .write imgs'将字符串内容写入缓冲
   .SaveToFile server.mappath(tofile),2'将缓冲的内容写入文件
   .Close()
  end with 
  set objstream=nothing
 end function
 
 
实现了保存文本数据和二进制数据,那就无所不能了*^_^*
 
附一个包括简单功能类完整代码:
---------------------------------------------------------------------------
Class xhttp
 private cset,sUrl
 Private Sub Class_Initialize()
  cset="UTF-8"
 end sub
 
 
 Private Sub Class_Terminate()
 End Sub

 Public Property LET URL(theurl)
  sUrl=theurl
 end property
 
 public property GET BasePath()
  BasePath=mid(sUrl,1,InStrRev(sUrl,"/")-1)
 end property
 
 public property GET FileName()
  FileName=mid(sUrl,InStrRev(sUrl,"/")+1)
 end property
 public property GET Html()
  Html=BytesToBstr(getBody(sUrl))
 end property
 private Function BytesToBstr(body)
  'Cset:GB2312 UTF-8
  dim objstream
  set objstream = Server.CreateObject("adodb.stream")
  with objstream
   .Type = 1 '设置返回数据类型为二进制
   .Mode = 3 '打开模式为读写
   .Open    
   .Write body  '将指定的数据装入对像中 body为内容
   .Position = 0 '指定对像内数据的当前指针
   .Type = 2  '设置返回数据类型为文本
   .Charset = Cset  '设定字符集类型
   BytesToBstr = .ReadText '取对象内的文本
   .Close
  end with
  set objstream = nothing
 End Function

 private function getBody(surl)
  dim xmlHttp
  set xmlHttp=server.createobject("MSXML2.XMLHTTP")
  xmlHttp.open "GET",surl,false
  xmlHttp.send
  if xmlHttp.readystate<>4 then
   exit function
  end if
   getBody=xmlhttp.responsebody
  set xmlHttp=nothing
 end function
 
 
 Public function saveimage(tofile)
  dim objStream,imgs
  imgs=getBody(sUrl)'取得图片的具休内容的过程
  Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO 2.5以上版本
  with objStream
   .Type =1'以二进制模式打开
   .Open
   .write imgs'将字符串内容写入缓冲
   .SaveToFile server.mappath(tofile),2'-将缓冲的内容写入文件
   .Close()
  end with 
  set objstream=nothing
 end function
 
end class 
 
 
TEST:
dim o
set o=new xhttp
o.saveimage "blue.gif"
set o=nothing
posted on 2005-11-17 21:53 登陆不起啦 阅读(1506) 评论(1)  编辑 收藏 引用 所属分类: 服务器端脚本

评论:
# re: [转]获取远程服务器数据(asp,adodb.stream,保存远程图片,XMLHTTP) 作者:blue1019 2007-10-10 18:03 | safe6
祝福你,天天快乐!  回复  更多评论
  
只有注册用户登录后才能发表评论。