Read Web page source code with XMLHTTP

Source: Internet
Author: User
Tags chr end string variable
xml| Web page | Source code in VBScript, we use the XMLHTTP component to get the source code of the Web page, and we can assign it to a variable to dynamically save it as an HTML file. For an introduction to the XMLHTTP object, please refer to the following article: http://www.webjx.com/htmldata/2005-10-17/1129556661.html; For examples of XMLHTTP, please refer to the following article: http:// Www.webjx.com/htmldata/2005-02-25/1109332374.html.
I wrote a piece of code following the introduction of the second article:
<%
Set X=server.createobject ("Microsoft.XMLHTTP")
X.open "Get", "http://www.webjx.com", False
X.send
Response.Write "<xmp>" &x.responseText& "</xmp>"
Set x=nothing
%>
However, after the test found that the Chinese are all garbled, to the internet search a bit, it is not find a simple solution. However, a responsebody attribute was found, which was not mentioned in the first article. Unlike the ResponseText property, Responsebody returns a binary string (ResponseText returns a regular string), with Response.BinaryWrite X.responsebody, found to be able to display Chinese correctly. So we just write a function that converts a binary string to a normal string bin2str:

function Bin2str (BIN)
Dim tmp,ustr
Tmp= ""
For I=1 to LenB (bin)-1
USTR=ASCB (MidB (bin,i,1))
If ustr>127 Then
I=i+1
TMP=TMP&AMP;CHR (USTR*256+ASCB (MidB (bin,i,1))
Else
TMP=TMP&AMP;CHR (USTR)
End If
Next
Bin2str=tmp
End Function

For information about restoring Chinese characters from a binary string please refer to the following article: Http://www.ahcit.com/200407/20040752.doc.
With this function, we can easily extract the source code of the specified URL and assign it to the variable:

Set X=server.createobject ("Microsoft.XMLHTTP")
X.open "Get", "http://www.webjx.com", False
X.send
Str=bin2str (X.responsebody)
Response.Write "<xmp>" &str& "</xmp>"
Set x=nothing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.