Rest-based Web Service Design

Source: Internet
Author: User
Tags representational state transfer
Rest-based Web Service Design

I have previously introduced the use of Apache axis to implement soap-based Web service implementation technology and related code. In general, although the soap web service solution is more mature and secure, however, the use threshold is high, and performance problems may occur in the case of high concurrency. it is not widely used on the Internet, so it is not suitable for Web 2.0 WebSite Services, at present, a large number of Web 2.0 websites use another solution-rest.

  Rest Architecture Design

Rest (representational state transfer) is a lightweight Web service architecture style, its implementation and operations are much more concise than soap and XML-RPC, can be fully implemented through HTTP protocol, the cache can also be used to increase the response speed. The performance, efficiency, and ease of use are superior to those of the SOAP protocol.

The rest architecture follows the crud principle. The crud principle only requires four actions for resources: Create (create), read (read), update (update), and delete (delete) you can complete the operations and processing. These four operations are an atomic operation, that is, an operation that cannot be further divided. They can be used to construct complex operation processes, just as the four arithmetic operations in mathematics are the most basic arithmetic operations of numbers.

The rest architecture allows people to really understand the original appearance of our network protocol HTTP, operations on resources, including obtaining, creating, modifying, and deleting resources, correspond to the get, post, put, and delete methods provided by HTTP, therefore, rest limits HTTP operations on a URL resource to get, post, put, and delete. This design and development method for network applications can reduce development complexity and improve system scalability.

  Rest Design Guidelines

The rest architecture is designed for web applications to reduce development complexity and improve system scalability. Rest proposes the following design principles:

Everything on the network is abstracted as a resource );

Each resource corresponds to a unique resource identifier );

Operate resources through the general connector interface (generic connector Interface;

Operations on resources do not change resource identifiers;

All operations are stateless ).

  Use rest Architecture

For developers, they are concerned about how to use the rest architecture. Here we will briefly discuss this issue. Rest is not only a new architecture, but also a new way of thinking in the Web development process: designing the system structure through URL. Rest is a simple design principle, an architectural style (or model), not a specific standard or architecture. There are many successful use cases of rest. The famous delicious and Flickr both provide rest-Based APIS, and client calls are extremely convenient, the following is a simple rest example I wrote using ASP. It can be seen that rest is so easy to use.

Client code:

Private function httpget (URL, method, data)
Dim XMLHTTP
Set XMLHTTP = server. Createobject ("msxml2.serverxmlhttp ")
XMLHTTP. Open method, URL + "? "+ Data, false
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"
XMLHTTP. setRequestHeader "Content-Length", Len (data)
XMLHTTP. Send (null)
If (XMLHTTP. Status = 200) Then httpget = XMLHTTP. responsetext
Set XMLHTTP = nothing
End Function

Private function httppost (URL, method, data)
Dim XMLHTTP
Set XMLHTTP = server. Createobject ("msxml2.serverxmlhttp ")
XMLHTTP. Open method, URL, false
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"
XMLHTTP. setRequestHeader "Content-Length", Len (data)
XMLHTTP. Send (data)
If (XMLHTTP. Status = 200) Then httppost = XMLHTTP. responsetext
Set XMLHTTP = nothing
End Function

Private function httpput (URL, method, data)
Dim XMLHTTP
Set XMLHTTP = server. Createobject ("msxml2.serverxmlhttp ")
XMLHTTP. Open method, URL, false
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"
XMLHTTP. setRequestHeader "Content-Length", Len (data)
XMLHTTP. Send (data)
If XMLHTTP. Status> = 400 and XMLHTTP. Status <= 599 then
Response. Write "error occurred:" & XMLHTTP. Status & "-" & XMLHTTP. statustext
Else
Response. Write XMLHTTP. responsetext
End if
If (XMLHTTP. Status = 200) Then httpput = XMLHTTP. responsetext
Set XMLHTTP = nothing
End Function

Private function httpdelete (URL, method, data)
Dim XMLHTTP
Set XMLHTTP = server. Createobject ("msxml2.serverxmlhttp ")
XMLHTTP. Open method, URL + "? "+ Data, false
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"
XMLHTTP. setRequestHeader "Content-Length", Len (data)
XMLHTTP. Send (null)
If XMLHTTP. Status> = 400 and XMLHTTP. Status <= 599 then
Response. Write "error occurred:" & XMLHTTP. Status & "-" & XMLHTTP. statustext
Else
Response. Write XMLHTTP. responsetext
End if
If (XMLHTTP. Status = 200) Then httpdelete = XMLHTTP. responsetext
Set XMLHTTP = nothing
End Function

Response. Write httppost ("http: // localhost/rest/service. asp", "Post", "Do = post ")
Response. Write httpget ("http: // localhost/rest/service. asp", "get", "Do = get ")
Response. Write httpput ("http: // localhost/rest/service. asp", "put", "Do = put ")
Response. Write httpdelete ("http: // localhost/rest/service. asp", "delete", "Do = Delete ")

Server code:

Response. Write Request. servervariables ("request_method ")
If (request. servervariables ("request_method") = "get") then
Response. Write "do get" + Request ("do ")
Elseif (request. servervariables ("request_method") = "Post") then
Response. Write "do post" + Request ("do ")
Elseif (request. servervariables ("request_method") = "put") then
Response. Write "do put" + Request ("do ")
Elseif (request. servervariables ("request_method") = "delete") then
Response. Write "Do Delete" + Request ("do ")
End if

By default, the IIS server does not support put and delete operations on ASP files. By default, the "403-Forbidden" error is returned. Therefore, you need to modify the IIS settings by using the following methods: choose "IIS Information Server"> "website"> "properties"> "main directory"> "application configuration"> "configuration"> "ing", and select "asp"> "edit"> "modify" to all actions.

For more information about rest, read the book restful Web Services.

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.