Web Service working mechanism based on Jave (5)

Source: Internet
Author: User
input = Socket.getinputstream ();
Output = Socket.getoutputstream ();
Await then creates a request object and invokes its parse method to parse the original HTTP request information.

Create Request object and parse
Request Request = new request (input);
Request.parse ();

Next, the await method creates a response object, uses the Setrequest method, and invokes its Sendstaticresource method.

Create Response Object
Response Response = new Response (output);
Response.setrequest (Request);
Response.sendstaticresource ();

Finally, await closes the socket. Call the request's GetURI method to check whether the URI of the HTTP request is a shutdown command. If it is, the shutdown variable is set to True and the program exits the while loop.

Close the socket
Socket.close ();
Check if the previous URI is a shutdown command
shutdown = Request.geturi (). Equals (Shutdown_command);

  Request Class

The request class represents an HTTP request. The socket handles the client's traffic, and returns a InputStream object that can be constructed to construct an instance of the request class by passing the object. The raw data for this HTTP request is obtained by calling the Read method of the InputStream object.

The Request has two public methods: Parse and GetURI. The parse method interprets the raw data for an HTTP request. It does not do many things----the only information it can take advantage of is the URI of the HTTP request, which is parseuri from the private method. The Parseuri method saves the URI to the URI variable, and then calls the public method GetURI to return the URI of an HTTP request.

To understand how the parse and Parseuri methods work, you need to know the internal structure of the HTTP request. This structure is defined in the RFC2616 document.

An HTTP request contains three parts:

Requested line (request lines)
Request Header (Headers)
Message body

Now, we are only interested in the first part of the HTTP request line (request lines). A request line starts with the method tag, followed by the URI and protocol version of the root request, and ends with the CRLF character. The element in the request line is separated by a space character. For example, the request line for the index.html file requested using the Get method is as follows:

get/index.html http/1.1//This is a request line
Method parse reads an entire byte stream from the InputStream of the socket, which is passed in by the Request object, and then parse stores the byte streams in a buffer. Assemble a StringBuffer object called request in the buffer.

The following listing 1.2 shows the use of the Parse method:

Listing 1.2. The Request class ' Parse method

public void Parse () {
Read a set of characters from the socket
StringBuffer request = new StringBuffer (2048);
int i;
byte[] buffer = new byte[2048];

try {
i = input.read (buffer);
}
catch (IOException e) {
E.printstacktrace ();
i =-1;
}

for (int j=0; j<i; j) {
Request.append ((char) buffer[j]);
}

System.out.print (Request.tostring ());
URI = Parseuri (request.tostring ());
}



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.