ASP 获取网页源码自动转换为中文

[浏览184]

'==================================================
'函数名:GetHttpPage
'作  用:获取网页源码
'参  数:HttpUrl ------网页地址
'参  数:Cset ---------网页编码类型
'==================================================
Function GetHttpPage(HttpUrl,Cset)
If IsNull(HttpUrl)=True Or Len(HttpUrl)<18 Or HttpUrl="$False$" Then
GetHttpPage="url错误"
Exit Function
End If
If IsNull(Cset)=True Or Cset="$False$" Then
Cset="GB2312"
end if
const TimeInterval=3 '设定时间间隔
const lResolve=5 '解析域名超时时间,秒
const lConnect=5 '连接站点超时时间,秒
const lSend=5 '发送数据请求超时时间,秒
const lReceive=15 '下载数据超时时间,秒
Dim Http
Set Http=server.createobject("MSXML2.ServerXMLHTTP")
http.setTimeouts lResolve*1000,lConnect*1000,lSend*1000,lReceive*1000
Http.open "GET",HttpUrl,False
http.setRequestHeader "User-Agent","Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)"
http.setRequestHeader "Pragma","no-cache"
http.setRequestHeader "Cache-Control","no-cache"
http.setRequestHeader "Connection","close"
On Error Resume Next
Http.Send()
Select Case http.readyState
Case 0
GetHttpPage="初始化失败"
Err.Clear
set http=nothing
Exit Function
Case 1
GetHttpPage="连接站点超时"
Err.Clear
set http=nothing
Exit Function
Case 2
GetHttpPage="服务器故障"
Err.Clear
set http=nothing
Exit Function
Case 3
GetHttpPage="数据下载超时"
Err.Clear
set http=nothing
Exit Function
Case 4
'下载成功
End Select
If http.status<>200  then
GetHttpPage="下载失败"&http.status&""
Err.Clear
set http=nothing
Exit Function
END IF
GetHTTPPage=bytesToBSTR(Http.responseBody,Cset)
Set Http=Nothing
If Err.number<>0 then
Err.Clear
End If
End Function
'==================================================
'函数名:BytesToBstr
'作  用:将获取的源码转换为中文
'参  数:Body ------要转换的变量
'参  数:Cset ------要转换的类型
'==================================================
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

关闭 ]
友情链接

Copyright © 2014 ASP技巧教程源码大全 All Rights Reserved
晋ICP备14006235号-3 页面执行时间:93.750毫秒 返回顶部