网络编程
位置:首页>> 网络编程>> JavaScript>> ASP获取网页内容(解决乱码问题)

ASP获取网页内容(解决乱码问题)

 来源:风之相随blog 发布时间:2009-07-26 10:44:00 

标签:ajax,乱码,asp

ASP使用xmlhttp获取远程网页内容,解决乱码问题

方法一:

<%
function getHTTPPage(url)
on error resume next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
  exit function
end if
getHTTPPage=bytes2BSTR(Http.responseBody)
set http=nothing
if err.number<>0 then err.Clear
end function
Function bytes2BSTR(vIn)
dim strReturn
dim i1,ThisCharCode,NextCharCode
strReturn = ""
For i1 = 1 To LenB(vIn)
  ThisCharCode = AscB(MidB(vIn,i1,1))
  If ThisCharCode < &H80 Then
   strReturn = strReturn & Chr(ThisCharCode)
  Else
   NextCharCode = AscB(MidB(vIn,i1+1,1))
   strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
   i1 = i1 + 1
  End If
Next
bytes2BSTR = strReturn
End Function
Response.Write(getHTTPPage(http://www.cidianwang.com))
%>

方法二:

 

<%
On Error Resume Next
Server.ScriptTimeOut=9999999
Function getHTTPPage(Path)
t = GetBody(Path)
getHTTPPage=BytesToBstr(t,"GB2312")
End function
Function GetBody(url)
on error resume next
Set Retri = CreateObject("Microsoft.XMLHTTP")
With Retri
.Open "Get", url, False, "", ""
.Send
GetBody = .ResponseBody
End With
Set Retri = Nothing
End Function
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
Response.Write(getHTTPPage(http://www.cidianwang.com))
%>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com