Website Optimization-XML + HTTP implementation

Source: Internet
Author: User

In the previous article, I introduced the basic knowledge of distributed systems. Here we will look at another HTTP Implementation of distributed implementation.

XML is used as the Data Exchange Protocol and HTTP is used for transfer. Lao Yao said that its performance will be higher than that of Web Services. I will try this test next time to see what the difference is.

Let's take a look at the implementation. first, we will introduce the major and realistic process. the client requests a server URL to establish an HTTP connection and then directly post a data stream to the server. The data stream can be XML, JSON, binary, and so on.

After the server receives the post stream, it will analyze what the client needs to do and complete the business functions after the analysis, the processing result is directly returned to the client. this is even if one call is completed.

Make a simple verification

1. XML data needs to be defined as the protocol between the communication parties, that is, the stream that the client needs to post to the server.

<? XML version = "1.0" encoding = "UTF-8"?>
<XMLHTTP>
<Userid> 744 </userid>

<Action> getpaylist </Action>
</XMLHTTP>

Assume that the above Protocol is the request that the client needs to post to the server. userid indicates the ID of the current user. The getpaylist table does not require the server to return a payment list to the client.

In order to reduce the function, we directly Save the content in the above section as a test. xml file.

Client postCodeAs follows:

Private string xml_http_stream ()
{
// Obtain the data stream from the past post

String data = gettextfromxmlfile (server. mappath ("test. xml "));
Byte [] bytes = encoding. utf8.getbytes (data );

// establish an HTTP connection with the server
httpwebrequest request = (httpwebrequest) webrequest. create ("http://xml.test.com/get.aspx");
request. method = "Post";
// request. proxy = NULL; If a service proxy needs to be added with the server code
request. contenttype = "text/XML";
request. contentlength = bytes. length;
stream requeststream = request. getrequeststream ();
// write a stream directly to the server over HTTP.

Requeststream. Write (bytes, 0, bytes. Length );
Requeststream. Close ();

// Wait for the server to process and return data
Webresponse response = request. getresponse ();
Stream responsestream = response. getresponsestream ();
Streamreader reader = new streamreader (responsestream, encoding. getencoding ("gb2312 "));
String STR = reader. readtoend ();
Reader. Close ();
Response. Close ();
Responsestream. Close ();
Response. Write (STR );
Return STR;

}

ServerGet. aspxThe Code accepted by the client is very simple. You can directly obtain the stream. If you need to analyze it, you can write your own code to analyze the post stream and then process the business logic and return it.

Page. response. contenttype = "text/XML ";
Streamreader reader = new streamreader (page. Request. inputstream );
If (reader! = NULL)
{
String xmldata = reader. readtoend ();
If (xmldata! = NULL & xmldata. length> 0)
{
Response. Write ("I got your post ");
Reader. Close ();
Reader. Dispose ();
}
}
Else
{
Response. Write ("no date post to me ");
}
If it is changed to JSON + HTTP, it is actually the same. Only the Communication Data Protocol is changed, not the XML format, but the JSON format. The result of data exchange is determined by the actual application.

Performance test comparison

Http://www.cnblogs.com/Leung/archive/2009/11/06/1597734.html

 

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.