取得远程url数据

<%
'用xml  
function GetSource(url)  
'取得url页面的数据.  
dim oXmlHttp,oStream  
set oXmlHttp=Server.CreateObject("Microsoft.XMLHTTP")  
oXmlHttp.open "GET",url,false  
oXmlHttp.send()  
set oStream=Server.CreateObject("ADODB.Stream")  
if isnull(oStream) then  
 Response.Write("您的机器不支持ADODB.Stream.")  
else  
 oStream.Type=1  
 oStream.Mode=3  
 oStream.Open()    
 oStream.Write(oXmlHttp.responseBody)  
 oStream.Position=0  
 oStream.Type=2  
 oStream.Charset="gb2312"  
 GetSource=oStream.ReadText()  
 if instr(GetSource,"800c0005")>0 then  
  GetSource=0 '连接失败  
 end if  
 if instr(GetSource,"Directory   Listing   Denied")>0 then  
  GetSource=0   'IIS返回  
 end if  
 oStream.Close()  
 set oStream=nothing  
end if  
end function
 
'*****************************************************************************************
'


'取得远程url数据
Function getHTTPPage(Path)    
 t=GetBody(Path)    
 getHTTPPage=BytesToBstr(t,"GB2312")    
End function
             
Function GetBody(url)      
 on error resume next    
 Set Retrieval=CreateObject("Microsoft.XMLHTTP")      
 With Retrieval      
 .Open "Get",url,False,"",""      
 .Send      
 GetBody=.ResponseBody    
 End With      
 Set Retrieval=Nothing      
End Function    

'调用XMLHTTP组件创建一个对象并进行初始化设置。    

Function BytesToBstr(body,Cset)    
 dim objstream    
 set objstream=Server.CreateObject("adodb.stream")    
 objstream.Type=1    
 objstream.Mode=3    
 objstream.Open    
 objstream.Write body    
 objstream.Position=0    
 objstream.Type=2    
 objstream.Charset=Cset    
 BytesToBstr=objstream.ReadText      
 objstream.Close    
 set objstream=nothing    
End Function    

Function Newstring(wstr,strng)    
 Newstring=Instr(lcase(wstr),lcase(strng))    
 if Newstring<=0 then Newstring=Len(wstr)    
End Function    

'处理抓取回来的数据调用adodb.stream组件并进行初始化设置。  
url="http://www.sohu.com"  
'获取指定页面的全部数据      
wstr=getHTTPPage(url)      
response.write wstr

 

'************************************************************************************************

 

 '该代码是访问远程的文件,并弹出提示下载的窗口,并且显示的来源可是你的站。
dim act,ifLogin,loginUrl,loginActUrl,loginData,fileUrl,fileName
act=request.QueryString("act")
if(act="do") then
 ifLogin=Cint(request.QueryString("ifLogin"))
 loginActUrl=request.Form("loginActUrl")
 loginData=request.Form("loginData")
 fileUrl=request.Form("fileUrl")
 fileName=mid(fileUrl,instrrev(fileUrl,"/")+1)
 extPos=instrrev(fileName,"?")
 if(extPos>0) then
  fileName=left(fileName,extPos-1)
 end if
 if(ifLogin=1) then
  call PostHttpPage(loginActUrl,loginData)
 end if
 call DownloadFile(fileUrl,fileName)
else
%>
<form name="form1" method="post" action="?act=do">
  <p>是否登录:
    <input name="ifLogin" type="radio" value="1">
    是
    <input name="ifLogin" type="radio" value="0" checked>
    否  </p>
  <p>登录提交地址:
    <input name="loginActUrl" type="text" size="50">
  </p>
  <p>
    登录数据:<input name="loginData" type="text" size="50">
  </p>
  <p>
    要保存的远程文件URL:
    <input name="fileUrl" type="text" size="50">
  </p>
  <p>
    <input type="submit" name="Submit" value="提交">
    <input type="reset" name="Submit2" value="重写">
  </p>
</form>
<%
end if
Function BytesToBstr(body,code)
        dim objstream
        set objstream = Server.CreateObject("adodb.stream")
        objstream.Type = 1
        objstream.Mode =3
        objstream.Open
        objstream.Write body
        objstream.Position = 0
        objstream.Type = 2
        objstream.Charset =code
        BytesToBstr = objstream.ReadText 
        objstream.Close
        set objstream = nothing
End Function
Function PostHttpPage(loginActUrl,PostData)
    Dim xmlHttp
    Dim RetStr     
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP") 
    xmlHttp.Open "POST", loginActUrl, False
    XmlHTTP.setRequestHeader "Content-Length",Len(PostData)
    xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"  
    xmlHttp.Send PostData
    If Err.Number <> 0 Then
        Set xmlHttp=Nothing
  response.Write("提交登录时出错!提交数据:"&PostData)
        Exit Function
    End If
    PostHttpPage=BytesToBstr(xmlHttp.responseBody,"GB2312")
    Set xmlHttp = nothing
End Function
'''''''''''''''''''''''''''''''''''''''''''
function DownloadFile(url,filename)
 Set xml = Server.CreateObject("Msxml2.XMLHTTP") '创建对象
   
   xml.Open "GET",url,False
   xml.Send '发送请求   
 
   if Err.Number>0 then
  Response.Status="404"
  else
  Response.ContentType="application/octet-stream"
  Response.AddHeader "Content-Disposition:","attachment; filename=" & filename
  Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)
  if Range="" then
   Response.BinaryWrite(xml.responseBody)
  else
   S.position=Clng(Split(Range,"-")(0))
   Response.BinaryWrite(xml.responseBody)
  End if
  end if
 Response.End 
   Set xml = Nothing
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
%> 
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值