XMLHttp ASP远程获取网页内容代码

asp下利用xmlhttp获取网页内容的方法这个方法一般比较通用的,然后通过字符截取网页的内容


url="http://www.csdn.net/"
wstr=getHTTPPage(url)
start=Newstring(wstr,"资源精选")
over=Newstring(wstr,"
")
body=mid(wstr,200,500)
response.write body
Function getHTTPPage(url)
dim objXML
set objXML=createobject("MSXML2.XMLHTTP")'定义
objXML.open "GET",url,false'打开
objXML.send()'发送
If objXML.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息
exit function
End If
getHTTPPage=bBytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码
set objXML=nothing'关闭
if err.number<>0 then err.Clear
End Function
Function Newstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
if Newstring<=0 then Newstring=Len(wstr)
End Function
Function bBytesToBstr(body)
dim objstream
set objstream = CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "gb2312"
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
bBytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function
Function BytesToBstr(body)
dim objstream
set objstream = CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "utf-8"
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
end Function



上一篇: 简单的PHP&MYSQL留言板
下一篇: 小偷软件
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2468
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭