For more information, see

Source: Internet
Author: User

For more information, see

I learned about javaWeb. Today I will focus on http requests. Should I review them!

I. Basic http concepts

1. What is http?

HTTP is short for Hyper Text Transfer Protocol. Its development is the result of cooperation between the World Wide Web Consortium and the Internet team IETF (Internet Engineering Task Force). They finally published a series of RFC, RFC 1945 defines HTTP/1.0. The most famous one is RFC 2616. RFC 2616 defines a common version of HTTP 1.1.

HyperText Transfer Protocol (Hyper Text Transfer Protocol) is a Transfer Protocol used to Transfer HyperText from a WWW server to a local browser. It makes the browser more efficient and reduces network transmission. It not only ensures that the computer transfers hypertext documents correctly and quickly, but also determines which part of the transmitted documents and which part of the content is first displayed (such as text before graphics.

2. http Response Model

The HTTP protocol always initiates a request from the client, and the server returns the response. See:

This restricts the use of the HTTP protocol and prevents the server from pushing messages to the client when the client does not initiate a request.

3. Workflow

 

An HTTP operation is called a transaction. The procedure can be divided into four steps:

 

1) First, the client and the server need to establish a connection. Click a hyperlink to start HTTP.

 

2) After a connection is established, the client sends a request to the server in the format of Uniform Resource Identifier (URL), Protocol version number, the MIME information is followed by the request modifier, client information, and possible content.

 

3) after receiving the request, the server returns the corresponding response information in the format of a status line, including the Protocol version number of the information, a successful or wrong code, MIME information is followed by server information, entity information, and possible content.

 

4) The information returned by the client receiving server is displayed on the user's display screen through a browser, and the client is disconnected from the server.

 

If an error occurs in one of the preceding steps, the error message is returned to the client and displayed. For users, these processes are completed by HTTP. Users only need to click and wait for the information to be displayed.

Ii. http request

2.1 content contained in HTTP requests

  After the client connects to the server, it requests a web resource from the server, which is called an HTTP request sent by the client to the server..

A complete HTTP request includes the following content:One request line, several message headers, and entity content
Example:

  

2.2. HTTP request details-Request Line

GET in the request line is called the request method. The request methods include POST, GET, HEAD, OPTIONS, DELETE, TRACE, and PUT. commonly used methods include GET and POST.
If the user does not set it, by default, the browser sends get requests to the server. For example, if the browser directly enters the address access, and the URL access requests are all get requests, if the user wants to change the Request Method to post, you can change the form submission method.
Whether POST or GET is used to request a WEB resource from the server, the difference between the two methods is mainly manifested in data transmission: if the request method is GET, you can use? Take the data that is handed over to the server. Multiple Data are separated by &, for example, GET/mail/1.html? Name = abc & password = xyzhttp/1.1
GET Mode features: the parameters attached to the URL address are limited, and the data capacity cannot exceed 1 K.
If the request method is POST, data can be sent to the server in the request's entity content. The Post method has the following features: the amount of data transmitted is unlimited.

2.3. HTTP request details-Message Header

Common Message Headers in HTTP requests

Accept: the browser uses this header to tell the server the data types it supports.
Accept-Charset: the browser uses this header to tell the server which character set it supports
Accept-Encoding: the browser uses this header to tell the server the supported compression format.
Accept-Language: the browser uses this header to tell the server its Language environment
Host: the browser uses this header to tell the server which Host to access.
If-Modified-Since: the browser uses this header to tell the server the time when data is cached.
Referer: the browser uses this header to tell the server which page the client uses for anti-leech protection.
Connection: the browser uses this header to tell the server whether the Connection is closed or what link is held after the request is complete.

For example:

1 Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, 2     application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*3 Referer: http://localhost:8080/JavaWebDemoProject/Web/2.jsp4 Accept-Language: zh-CN5 User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)6 Accept-Encoding: gzip, deflate7 Host: localhost:80808 Connection: Keep-Alive

 

3. HTTP response 3.1 and content included in HTTP Response

  An HTTP response indicates the data that the server sends back to the client.It includes a status line, several message headers, and entity content.

  
Example:

 1 HTTP/1.1 200 OK 2 Server: Apache-Coyote/1.1 3 Content-Type: text/html;charset=ISO-8859-1 4 Content-Length: 105 5 Date: Tue, 27 May 2014 16:23:28 GMT 6  7 3.2. HTTP Response Details-status line

Status row format:Cause description of HTTP Version Status Code <CRLF>
Example: HTTP/1.1 200 OK
The status code is used to indicate the server's request processing result. It is a three-digit decimal number. The response status codes are classified into five categories, as shown below:
  

3.3 HTTP Response Details-Common Response Headers

Common Response Headers (message headers) in HTTP responses)
Location: the server uses this header to tell the browser where to jump
Server: the Server uses this header to tell the browser the Server model.
Content-Encoding: the server uses this header to tell the browser about the data compression format.
Content-Length: the server uses this header to tell the browser the Length of the data to be sent back.
Content-Language: the server uses this header to tell the browser Language environment
Content-Type: the server uses this header to tell the browser the Type of data to be sent back.
Refresh: the server uses this header to tell the browser to regularly Refresh
Content-Disposition: the server uses this header to tell the browser to download data.
Transfer-Encoding: the server uses this header to tell the browser that the data is sent back in blocks.
Expires:-1 control browser not to cache
Cache-Control: no-cache
Pragma: no-cache

4. Set a response header on the server side to control the browser behavior of the client. 4.1. Set a Location response header to redirect requests.

 

1 import javax. servlet. http. httpServletRequest; 2 import javax. servlet. http. extends; 3 4 public class ServletDemo01 extends HttpServlet {5 private static final long serialVersionUID = 1L; 6 protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {7 this. doPost (request, response); 8} 9 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {10/** 11 * sets the server status response code: request the resource to move the new address 12 */13 response. setStatus (302); 14/** 15 * tell the browser where to jump by setting the Location, the so-called request redirection 16 */17 response. setHeader ("Location", "/http_study/1.jsp"); 18} 19 20}

When the URL "http: // localhost: 8080/http_study/ServletDemo01" is used in the browser to access ServletDemo01, the server returns a 302 status code to tell the browser that you do not have any resources, however, I use the Location response header to tell you where the response header is located, and the browser parses the Response Header Location to know that it is going to jump to // http_study/1. jsp page, so it will automatically jump to 1.jsp, as shown in:

 

 

4.2 set the Content-Encoding response header to tell the browser the data compression format

 

1 package servlet; 2 3 import java. io. byteArrayOutputStream; 4 import java. io. IOException; 5 import java.util.zip. GZIPOutputStream; 6 import javax. servlet. servletException; 7 import javax. servlet. http. httpServlet; 8 import javax. servlet. http. httpServletRequest; 9 import javax. servlet. http. expiration; 10 11 public class ServletDemo02 extends HttpServlet {12 private static final long serialVersionUID = 1L; 13 14 protected void doGet (HttpServletRequest request, 15 response) throws ServletException, IOException {16 this. doPost (request, response); 17} 18 19 protected void doPost (HttpServletRequest request, 20 HttpServletResponse response) throws ServletException, IOException {21 String data = "I am asadfdsf I am asadfds" 22 + "f I am asadfdsf I am asadfdsf" 23 + "I am asadfdsf I am" 24 + "asadfdsf I am asadfd" 25 + "sf I am asadfdsf I am as" 26 + "adfdsf I am asadfdsf I am" 27 + "outputs I am asadfdsf" 28 + "I am asadfdsf I am asadfd" 29 + "sf I am asadfdsf I am asa" 30 + "dfdsf "my name is adfdsf. I'm a "31 +" sadfdsf. I'm asadfdsf. I'm asadfdsf. I'm "32 +" asadfdsf. I'm asadfdsf. I'm asadfdsf. I am asadfdsf, I am "34 +" asadfdsf, I am "35 +" asadfdsf, I am asadfdsf, I am a "36 +" sadfdsf I am asadfdsf and I am asadfdsf "; 37 38 System. out. println ("pre-compression size:" + data. getBytes (). length); 39/** 40 * Start compression 41 */42 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 43 GZIPOutputStream gout = new GZIPOutputStream (bout); 44 gout. write (data. getBytes (); 45 gout. close (); 46 // obtain the compressed data 47 byte [] B = bout. toByteArray (); 48 response. setHeader ("Content-Encoding", "gzip"); 49 response. setHeader ("Content-Length", B. length + ""); 50 response. getOutputStream (). write (B); 51 52} 53 54}
Here, GZIPOutputStream is used to compress data.
When using the URL address "http: // localhost: 8080/http_study/ServletDemo02" in the browser to access ServletDemo02, you can see the following request header information:

4.3 set the content-type response header and specify the type of the return data

The data types that the browser can receive (Accept) include:
Application/x-ms-application,
Image/jpeg,
Application/xaml + xml,
Image/gif,
Image/pjpeg,
Application/x-ms-xbap,
Application/vnd. ms-excel,
Application/vnd. ms-powerpoint,
Application/msword,

1 package servlet; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. io. outputStream; 6 7 import javax. servlet. servletException; 8 import javax. servlet. http. httpServlet; 9 import javax. servlet. http. httpServletRequest; 10 import javax. servlet. http. httpServletResponse; 11 12 public class ServletDemo03 extends HttpServlet {13 private static final long serialVersionUID = 1L; 14 p Rotected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {15 this. doPost (request, response); 16} 17 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {18 19 // use content-type to specify the Data type sent to the browser as image/release 20 response. setHeader ("content-type", "image/jpeg"); 21 // get stream 22 InputStream in = r Equest. getServletContext (). getResourceAsStream ("/images/1.jpg"); 23 // InputStream in = new InputStream ("/images/1.jpg"); 24 byte [] B = new byte [1, 1024]; 25 int ln = 0; 26 OutputStream OS = response. getOutputStream (); 27 while (ln = in. read (B ))! =-1) {28 OS. write (B, 0, ln); 29} 30} 31 32}

The response is

The running result is as follows:

4.4 set the content-disposition response header to allow the browser to download files
Package servlet; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class ServletDemo04 extends HttpServlet {private static final long serialVersionUID = 1L; protected void doGet (HttpServletReques T request, HttpServletResponse response) throws ServletException, IOException {this. doPost (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/*** sets the content-disposition response Header, let the browser download the file */response. setHeader ("content-disposition", "attachment; filename =" + "1.jpg"); InputStream in = request. getServletContext (). getRes OurceAsStream ("images/1.jpg"); byte [] B = new byte [1024]; OutputStream OS = response. getOutputStream (); int len = 0; while (len = in. read ())! =-1) {OS. write (B, 0, len );}}}

Access localhost: 8080/http_study/ServletDemo04 in the browser. The file download box is displayed, as shown in:

 

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.