Use of XMLHTTP

Source: Internet
Author: User

Use of XMLHTTP

As the name implies, XMLHTTP is a hypertext transfer protocol for transmitting XML data.

In fact, the data transmission process of XMLHTTP is more flexible:
It can upload instructions in XML format, strings, streams, or an unsigned integer array. It can also be a URL parameter.
The output result can be XML data, a string, a stream, or an unsigned integer array.
For more information, see the link at the end of the article.

The client calls XMLHTTP in five steps:
1. Create an XMLHTTP object
2. Open the connection with the server, and define the command sending method, Service webpage (URL), and request permissions.
The client opens a connection to the Service webpage of the server through the Open command. Like normal HTTP Command Transmission, you can use the "GET" method or "POST" method to direct to the Service webpage of the server.
3. Send commands.
4. Wait for and receive the processing results returned by the server.
5. Release the XMLHTTP object

XMLHTTP method:
Open bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword
BstrMethod: data transmission method, that is, GET or POST.
BstrUrl: the URL of the Service webpage.
VarAsync: whether to run the command synchronously. The default value is True, that is, synchronous execution, but can only be performed in the DOM.
In applications, set it to False, that is, asynchronous execution.
BstrUser: user name, which can be omitted.
BstrPassword: user password, which can be omitted.

Send varBody
VarBody: instruction set. It can be XML data, a string, a stream, or an unsigned integer array. It can also be omitted, so that the command can be substituted by the URL parameter of the Open method.

SetRequestHeader bstrHeader, bstrValue
BstrHeader: HTTP header)
BstrValue: HTTP header value
If the Open method is defined as POST, you can define form upload:
Xmlhttp. setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

XMLHTTP attributes:
Onreadystatechange: obtains the event handle of the returned result in synchronous execution mode. It can only be called in the DOM.
ResponseBody: returns an unsigned integer array.
ResponseStream: The returned result is an IStream stream.
ResponseText: returns a string.
ResponseXML: The returned result is in XML format.


The following is an example of an application in the source program appended to this article:
Function GetResult (urlStr)
Dim xmlHttp
Dim retStr

Set xmlHttp = CreateObject ("Msxml2.XMLHTTP") create an object
On Error Resume Next Error handling
XmlHttp. Open "POST", urlStr, and False enable the connection in POST mode and execute the connection asynchronously.
XmlHttp. setRequestHeader "Content-Type", "application/x-www-form-urlencoded" Upload form
XmlHttp. Send

If Err. Number = 0 Then If the connection is correct
RetStr = xmlHttp. responseText
Else
RetStr = "Url not found" otherwise, an error message is returned.
End If
Set xmlHttp = nothing release object
GetResult = retStr returned results
End Function

The GetResult () function brings the URL parameter of a Service webpage and places the Upload Command on the parameter following the URL, for example:
UrlStr = "server. asp? Cmd = "& cmd &" & db = "& db &" table = "& table
Cmd: Execution method, such as query, modification, and deletion.
Db: name of the server database
Table: the name of the server table.

Then, submit the command and wait for and receive the returned processing result. The result is returned as a string.

Finally, the function caller processes and displays the result.

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.