Data interaction of clients and servers

zhaozj2021-02-16  79

Author: Zhang Jian

1. Communication mode of C / S We discuss the data interaction of the client and server in the previous article, the direction of the data stream is always from the server to the client, and rarely involves the client machine to send requests and handle the return information. problem. In fact, in e-commerce, customers send data to the server is also an important link, such as the user who fills in the goods or the like. In a conventional implementation, the user ends as long as it makes a small change in the order, to send messages to the server, requiring update data. This increases the load of the server and the network, which reduces work efficiency. More efficient work mode is to cache changes to the changed information in the client side, then send it to the server separately, so some uncertain modification information is stored in the client, only those data that need to be updated will be updated. Send it out, thereby avoiding many unnecessary operations of the network and servers. 2. Communication based on XML-based C / S using XML is a highly efficient manner. First pack the XML data on the client, then send it to the server as a unit in the XML packet, the server returns the message after processing the data, and the client receives the message to perform other operations, thereby ending a communication cycle. The specific implementation steps are as follows: ● The client makes an XMLDOM object as a carrier that sends XML data; ● The client creates an XMLHTTP object, including a variety of methods and properties, can send XML data to the server (such as ASP) Page), ready to receive response information; ● The client reproduces the XML packet to the XMLHTTP object and sends it to the ASP page; ● The server performs an ASP, and create a server-side XMLDOM object to receive XML data; ● ASP puts the data package On the XMLDOM object of the server; ● ASP performs the necessary processing for XML data, and returns a confirmation message; ● The client receives the response message and performs the next step. 3. The primary task of sending a data client to the server is to construct an XML packet. XMLDOM as a carrier of the packet has a source of data such as any XML document or a fragment of the XML document (such as XML data island), and can even use the loadXML method to receive the user input information XML document. Below is a dynamically generated XML document: set docsubmit = createObject ("Microsoft.xmLDom") DOCSUBMIT.ASYNC = false docsubmit.LoadXML "" & "" & " "& Txtcustomerid.value &" "&" "If the user enters" 5 "as the user ID, then the result of the above program is as follows: 5 Next, add XML data to the element of the DOM tree.

For example, there is an XML data island as follows: 14.99 2 Using the DOM technology described earlier Access element: set docorder = dsoorder.xmldocument set nodeorder = Docorder .selectsinglenode ("// order") element is part of the DSoORDER data island, to add it to other DOM trees (such as the previous XML packet), you must put elements and all its The subsequent node is backed up because the appendchild method deletes the object from the current DOM tree after adding the operation object to another DOM tree! So you must call the ClonEnode method: set nodeordosendosend = nodeorder.Clonenode (true) DOCSUBMIT.DocumentEndEnd.Appendchild NodeRDertosnd After the above operation, the last formed packet is: 5 14.99 2 4, the Open method of the XMLHTTP object After the configuration is completed, you can send the packet to the server using the HTTP Request object. Microsoft.xmlHTTP objects are provided in MSXML to complete the conversion from packets to the REQUEST object, and send tasks. Creating the statement of the XMLHTTP object is as follows: set Poster = CreateObject ("Microsoft.xmlhttp") Object ("Microsoft.xmlhtttp") Object Call Open method Initialize the REQUEST object, the syntax format is: poster.open http-method, url, asken http-method, url, async, userid, Password Open method It contains five parameters, the top three are necessary, and then two are optional (provided when the server needs to be authenticated). The meaning of the parameters is as shown in the following table: Parameter Description HTTP-Method HTTP communication mode, such as a URL address of the server that receives XML data. The ASP or CGI program async is usually indicated in the URL, indicating whether the request is asynchronous. If it is an asynchronous communication method, the client will not wait for the server's response; if it is synchronous mode, the client will wait until the server returns the message to perform other action userid user IDs, used for server authentication Password user password, for servers Authentication

In the following example, the client sent a POST request to the "CUSTOMERORDER.ASP" page using an asynchronous manner: Poster.Open "Post", "CustomerORDER.ASP", false1, XMLHTTP object Send method to use the open method to Request After the object is initialized, call the Send method to send XML data: The parameter type of the POSTER.SEND XML-DATA Send method is Variant, which can be a string, a Dom tree or any data stream. The way to send data is divided into synchronous and asynchronous. In an asynchronous mode, once the data package is sent, end the Send process, the client performs other operations; and in the synchronization mode, the client will wait until the server returns a confirmation message to end the Send process. The ReadyState property in the XMLHTTP object reflects the progress of the server during processing the request. The program of the client can set the corresponding event processing method according to this status information. The attribute value and its meaning are shown in the following table: Value Description 0 Response object has been created, but the XML document upload process has not ended 1 XML document has been loaded 2 XML document has been loaded, and the 3 part XML document has parsed 4 documents already After the analysis is complete, the client can accept the return message

2. After receiving the packets sent by the client, the server-side data processing server will immediately process the data and make a corresponding response. The server first creates an XMLDOM object and then loads the data in the Request object and starts accessing XML data via an XMLDOM object. After obtaining XML data, the first thing to do is to verify the XML document (this part of the specific process we will introduce the XML Schema when it will be described later). Once verified, the DOM interface can be used to analyze the XML data (for example, using the extracted information to update records in the database). A simple ASP script as follows: <% Set docReceived = CreateObject ( "Microsoft.XMLDOM") docReceived.async = False docReceived.load Request Set rootNode = docReceived.documentElement Set nodeCustomer = docReceived.selectSingleNode ( "// customer") customerID = NodeCustomer.firstchild.nodeValue ......%> 3, the server-side response message server is constructed after processing XML data, and returns to the client. The form of the message can be a plain text, an HTML page, an XML document, or an HTML page embedded in XML data island. First, let's take an example of an HTML page. This message page contains information about the customer ordered:

confirmation of order <% for each node in listorderItem title = node.getattribute ("title") set quantityNode = Node.selectsinglenode ("Quantity") Quantity = QuantityNode.FirstChild.NodeValue%>

<% = title%>, <% = quantity%> <% next%> The server uses XML documents as return messages The advantage is that the client can use the smart program to analyze structured messages, and more accurately understand the information to be expressed by the server. For example: <% Set docResponse = CreateObject ( "Microsoft.XMLDOM") docResponse.async = False docResponse.load "MyFixedResponse.xml" Response.ContentType = "text / xml" Response.save docResponse%> in the use of XML documents, you must The value of the contentType property is specified as "text / XML" before filling the response content, indicating the format of the response message is XML. The use of the Save method is to populate the XML document content into the Response object.

转载请注明原文地址:https://www.9cbs.com/read-14065.html

New Post(0)