Compared to AJAX, the server XMLHTTP is to use XMLHTTPREQUEST objects on the server. Although it is inconvenient to use asynchronous requests on the server side, as a component that can send HTTP requests as a server, there is no harm to study.
Here, I am talking about using the server XMLHTTPREQUEST in the ASP environment, and uses JScript as the language of a demonstration code. Therefore, you need to understand ASP and JSCRIPT.
The server XMLHTTP is usually used to obtain the webpage or other content of the remote host. The news aggregation system is generally using the server XMLHTTP object to obtain the content of the aggregated Feed, and then use the XMLDOM object to analyze the content of Feed and take out the title of the news. , Author, content and other information, there are then existing in the database, and then show the news of several data sources together. Capturing shrimp is such a news aggregate, but it is not written in ASP :)
In ASP, we can use the following code to create a serverxmlhttp object, and this object is the basis for our operation on the server.
var xmlhttp = New Activexobject (msxml2.serverxmlhttp.5.0);
First of all, to understand what are the more useful methods of the serverxmlhttp object:
1. Abort this method is used to cancel the request of XMLHTTP. If the XMLHTTP object sends the request asynchronous, if the request does not return to a certain time, you can use this method to cancel the request.
2. The return value of the GetallResponseheaders is a string, which is equivalent to the head of the HTTP request removed the request method, URI, and protocol version information.
3. getResponseheader This method is used to obtain the specified head information. It is more useful that it can be used to obtain the Content-Type, Referer, etc. of the returned data.
4. Open uses a specified request method, URI and synchronization method, and authentication information to initialize a request.
5. Send send HTTP requests, wait for receiving response data. Note that if the request is sent in synchronization, the Send method will not return immediately after calling. Instead, it will return after the request is completed. Return immediately. In addition, the Send method has a optional parameter Body, indicating that the data to be sent is more useful when using the POST method.
6. SettimeOut Set the 4out time of the serverxmlhttp object, namely: domain name analysis, connecting server, sending data, receiving response. You can control the serverxmlhttp object by setting the corresponding timeout time, so as to prevent the serverxmlhttp from returning in time and cause the program to stop responding.
7. SetRequestHeader sets the header of the request. In the client XMLHTTPREQUEST, the data type that is usually used to set the request, or the method of identifying requests, etc. For example, JQuery will increase the head identifier X-Request-With, indicating that the request is from the XMLHTTPRequest object. Send it to facilitate the server to make corresponding actions.
8. WaitForresPonse can use this method to control the request process when sending requests asynchronous. In the server script, you must not directly use the callback function to control asynchronous requests like a client, and there is no corresponding function to use the program to sleep for a certain time. Therefore, in order to wait for the request to return, we can use this method to wait for a certain period of time to take a certain time for a certain time. Essence
In addition, there are other methods, such as GetOption, SetOption, SetProxy, etc. These methods are used less, so it is not introduced here. Friends who need to know can check MSDN.
Next, look at the attributes of the Serverxmlhttp object:
1. OnReadyStateChange XMLHTTP object State changes to the callback function. This attribute lays a foundation for the asynchronous operation, which can allow the program to know whether the xmlhttp operation has been completed without querying the status of the XMLHTTP object.
2. ReadyState XMLHTTP object status, with 5 values, from 0 to 4, which means:
0 -Not initialization, just use New ActivexObject (msxml.serverxmlhttp.5.0) when creating
1 -During the load, at this time, the Open method has been called, but the data has not been used to send data
2 -have been loaded, and the send method has been called to send data, but there is no response flow that can be available
3 -I am interacting and is receiving data. At this time, you can use Responsebody and Responsetext properties to obtain some of the data obtained
4 -Complete the request, all the data has been accepted to complete
Under normal circumstances, we only need to judge Status 4. At this time, the data is all loaded. Use Responsebody or Responsetext property to obtain the required data.
3. Status HTTP response status code. The normal situation should be 200. If the requested resources do not exist, it will return 404, and other status codes such as server error 500.
4. Statustext HTTP response status text to describe the meaning of the response status code, such as OK in 200 OK, 404 Not Found
5. Responsebody responds to the byte array of data, which can be used directly in VBScript, but it needs to be converted in JScript.
6. Responsetext Get the response data by text
7. Responsexml returns the response data as an XMLDOM object. This is especially useful when the request data is a XML document
8. ResponSestream response flow object, this attribute is not commonly used