欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-18 11:14:27
复制代码 代码如下:url="http://www.csdn.net/" wstr=gethttppage(url) start=newstring(wstr,"资源精选&...

复制代码 代码如下:

url="http://www.csdn.net/"
wstr=gethttppage(url)
start=newstring(wstr,"资源精选<!-- 下载 -->")
over=newstring(wstr,"<div class=""friendlink"">")
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