XMLHTTP-Introduction (Automatic Webpage content capture)

Source: Internet
Author: User
MSXML provides Microsoft. XMLHTTP objects, which can complete conversion from data packets to request objects and send tasks.
The statement for creating an XMLHTTP object is as follows:
Set objxml = Createobject ("msxml2.xmlhttp ")Or
Set objxml = Createobject ("Microsoft. XMLHTTP ")
'Or, for version 3.0 of XMLHTTP, use:
'Set xml = server. Createobject ("msxml2.serverxmlhttp ")
After the object is created, the open method is called to initialize the request object. The syntax format is:
Poster. Open http-method, URL, async, userid, password

The open method contains five parameters, the first three are necessary, and the last two are optional (provided when the server requires authentication ). The parameter meanings are as follows:
HTTP-method: the communication method of HTTP, such as get or post.
URL: the URL of the server that receives XML data. The ASP or CGI program is usually specified in the URL.
Async: A boolean identifier indicating whether the request is asynchronous. If it is an asynchronous communication method (true), the client will not wait for the server's response; if it is a synchronous mode (false), the client will wait until the server returns a message to execute other operations
Userid, used for Server Authentication
Password, used for Server Authentication
Send method of XMLHTTP object
After initializing the request object using the open method, call the send method to send XML data:
Poster. Send XML-Data
 
The parameter type of the send method is variant, which can be a string, DOM tree, or any data stream. Data transmission methods can be synchronous or asynchronous. In asynchronous mode, once a packet is sent, the send process is terminated and the client performs other operations. In synchronous mode, the client waits until the server returns a confirmation message to terminate the send process.
The readystate attribute in the XMLHTTP object can reflect the progress of the server when processing requests. The client program can set corresponding event handling methods based on the status information. The attribute values and their meanings are shown in the following table:
Value description
0 response object has been created, but the XML Document Upload process has not been completed
1. the XML document has been loaded.
2. the XML file has been loaded and is being processed.
3. Some XML documents have been parsed.
4. The file has been parsed and the client can accept the returned message.
The client processes response information.
After the client receives the returned message, it simply processes it and basically completes an interaction cycle between C/S. The client receives the response through the attributes of the XMLHTTP object:
● Responsetxt: Use the returned message as a text string;
● Responsexml: regards the returned message as an XML document, which is used when the server response message contains XML data;
● Responsestream: treats the returned message as a stream object.

------ The following simple JavaScript function send (STR, URL )---------------
Xmldom and XMLHTTP objects are used. The advantages of this technology are: Full JS control, convenient/simple, compared with RDS
Or remote is much better (premise: both the server and client must install ie5 or later ).
This technology is also used by the online information function of Brushless newest. If you are interested, please check it out ..
Function send (STR, URL)
// The STR parameter is the imported XML data. You can also input other text data.
// However, this function requires the server to process and then return XML data. You can also modify it.
// The URL parameter indicates the ASP file address of the data to be processed.
{
VaR HTTP = new activexobject ("Microsoft. XMLHTTP") // create an XMLHTTP object
VaR dom = new activexobject ("Microsoft. xmldom") // create an xmldom object
HTTP. Open ("Post", URL, false)
// The first parameter indicates that data can be sent in the "Post" mode, which can be 4 MB in size or changed to "get". Only KB is allowed.
// The 2nd parameter indicates the file to which the data is sent for processing.
// The 3rd parameters mean synchronous or asynchronous mode. True indicates asynchronous mode, and false indicates synchronous mode.
HTTP. Send (STR) // start to send data ..
Dom. async = false // set it to synchronous to get data
Dom. loadxml (HTTP. responsetext)
// Start to obtain the data returned by the server after processing. Here, I set the data to XML; otherwise, an error occurs.
// You can modify it yourself. returns binary or record set data .................................
If (DOM. parseerror. errorcode! = 0) // check whether an error occurred while obtaining data
{
Delete (HTTP)
Delete (DOM)
Return (false)
}
Else
{
VaR back = dom.doc umentelement. childnodes. Item (0). Text
// Get the returned XML data. Here we assume that the handler only returns one XML data row (one node)
Delete (HTTP)
Delete (DOM)
Return (back) // The function returns data...
}
}
 
VaR cat = Send ("<user information> <Name> Xie lemon </Name> </user information>", "http: // www. chinaasp. COM/viva. ASP ") // execute the Function
If (cat = false)
{
Alert ("sorry. The Handler returns false. Data Processing has failed ........")
}
Else
{
If (eval (CAT ))
{
Alert ("OK. The data has been sent successfully. Processing is complete !!!!!! ")
}
Else
{
Alert ("sorry. The Handler returns false. Data Processing has failed ........")
}
}
 
================================ Viva. ASP ================================
On Error resume next
Dim Bobo
Dim Momo
Set Bobo = server. Createobject ("Microsoft. xmldom ")
Bobo. async = false
Bobo. Load request
If Bobo. parseerror. errorcode <> 0 then
Response. Write ("<program processing result> <final result> false </final result> </program processing result> ")
Else
Set Momo = Bobo. documentelement
If Momo. childnodes. Item (0). Text = "thank lemon" then
Response. Write ("<program processing result> <final result> true </final result> </program processing result> ")
Else
Response. Write ("<program processing result> <final result> false </final result> </program processing result> ")
End if
End if
Set Bobo = nothing
The following is an example of practical application.

 <%

Dim objxmlhttp, XML
Set xml = server. Createobject ("Microsoft. XMLHTTP") 'creates an object

XML. Open "get", "http://www.pconline.com.cn/pcedu/emp... 404/366680. html", false ''sets the object. For details about how to use XMLHTTP, see: http://www.cnsxml.com/blogview.asp? Logid = 273 XMLHTTP object and Method

XML. Send' send request

Response. addheader "content-disposition", "attachment?filename=*ell-pres.zip" 'Add a header to this file

Response. contenttype = "application/zip" 'set the output type

Response. binarywrite XML. responsebody 'outputs binary data to the browser
Response.

Set xml = 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.