ASP中利用XMLHTTP获取并输出远程网页

作者:Hily 原始链接:http://hily.me/blog/2004/04/asp-xmlhttp-fetch/
版权声明:可以转载,转载时务必以超链接形式标明文章原始出处作者信息版权声明

代码如下:

Function URLEncoding(vstrIn)
    strReturn = ""
    For i = 1 To Len(vstrIn)
        ThisChr = Mid(vStrIn,i,1)
        If Abs(Asc(ThisChr)) < &HFF Then
            strReturn = strReturn & ThisChr
        Else
            innerCode = Asc(ThisChr)
            If innerCode < 0 Then
                innerCode = innerCode + &H10000
            End If
            Hight8 = (innerCode  And &HFF00)\ &HFF
            Low8 = innerCode And &HFF
            strReturn = strReturn & "%" & Hex(Hight8) &  "%" & Hex(Low8)
        End If
    Next
    URLEncoding = strReturn
End Function

Function bytes2BSTR(vIn)
    strReturn = ""
    For i = 1 To LenB(vIn)
        ThisCharCode = AscB(MidB(vIn,i,1))
        If ThisCharCode < &H80 Then
            strReturn = strReturn & Chr(ThisCharCode)
        Else
            NextCharCode = AscB(MidB(vIn,i+1,1))
            strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
            i = i + 1
        End If
    Next
    bytes2BSTR = strReturn
End Function

set oReq = CreateObject("MSXML2.XMLHTTP")
oReq.open "POST","http://www.xmu.edu.cn/",false
oReq.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
oReq.send
Response.Write bytes2BSTR(oReq.responseBody) 

修改代码1:
发现把最后一句改为 Response.BinaryWrite oReq.responseBody 也是可以的。

修改代码2:
结合了ADODB.Stream,优化了B2S函数:

Function bin2str2(binstr)
Dim BytesStream,StringReturn
Set BytesStream = CreateObject("ADODB.Stream")
With BytesStream
  .Type = 2
  .Open
  .WriteText binstr
  .Position = 0
  .Charset = "GB2312"
  .Position = 2
  StringReturn = .ReadText
  .close
End With
Set BytesStream = Nothing
bin2str2 = StringReturn
End Function

-- EOF --

发表一下您的高见

If you have any question, or for the language problem, please fell free to leave a comment or just contact me with email: hilyjiang [At] Gmail.