Java Basics---HTTP protocol

Source: Internet
Author: User

HTTP protocol has been its own weak point, and did not take too much time to see this aspect of the content, today, the mood came on the Internet search on the HTTP protocol, found that a friend wrote a very good blog, Bowen address: (http://www.cnblogs.com/xdp-gacl/p/ 3751277.html) aloof and pale wolf

First, what is the HTTP protocol

HTTP is shorthand for the Hypertext Transfer Protocol (Hypertext Transfer Protocol), an application-layer protocol to the TCP/IP protocol that defines the process of exchanging data between a Web browser and a Web server. After the client connects to the Web server, if you want to obtain a Web resource in the Web server, you need to follow a certain communication format, the HTTP protocol is used to define the format of the client and Web server communication.

Ii. version of the HTTP protocol

Version of the HTTP protocol: http/1.0, http/1.1

Iii. the difference between HTTP1.0 and HTTP1.1

In the HTTP1.0 protocol, after a client has established a connection with a Web server, only one Web resource can be obtained.
In the HTTP1.1 protocol, when a client connects to a Web server, it obtains multiple Web resources on one connection.

Iv. HTTP Request 4.1, HTTP request includes content

  after the client connects to the server, it requests a Web resource from the server, which is called the client sends an HTTP request to the server .

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

4.2. Details of HTTP request--Request Line

The get in the request line is called the request method: POST,, OPTIONS, DELETE, TRACE, PUT, commonly used are: GET, POST
If the user is not set, by default the browser sends a GET request to the server, for example, in the browser direct output address access, point hyperlink access, etc. are get, the user wants to change the request mode to post, can be implemented by changing the form's submission method.
Regardless of whether post or get is used to request a Web resource to the server, the difference between the two approaches is mainly in the data delivery: If the request is a Get mode, you can bring the data to the server in the form of the requested URL address, separating multiple data with &. Example: Get/mail/1.html?name=abc&password=xyz http/1.1
The feature of Get mode is that the parameters accompanying the URL address are limited and the data capacity is usually not more than 1K.
If the request is in the form of post, you can send data to the server in the requested entity content, with the characteristics of post: Unlimited amount of data transferred.

4.3. Details of HTTP request--Message header

Common message headers in HTTP requests

Accept: The browser tells the server through this header which data types it supports
Accept-charset: The browser tells the server through this header which character set it supports
Accept-encoding: Browser Through this header to tell the server, the supported compression format
Accept-language: The browser tells the server through this header that its language environment
Host: The browser tells the server through this header which host to access
If-modified-since: The browser tells the server through this header that the time to cache the data
Referer: The browser through this header to tell the server, the client is which page to the anti-theft chain
Connection: Browser Through this header to tell the server, after the request is broken link or how to hold the link

For example:

Accept: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, */*referer:http:// localhost:8080/javawebdemoproject/web/2.jspaccept-language:zh-cnuser-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) Accept-encoding:gzip, deflatehost:localhost:8080connection:keep-alive
V. HTTP response 5.1, content included in HTTP response

  an HTTP response represents the data that the server sends back to the client , including a status line, several message headers, and entity content.

Example:

http/1.1 Okserver:apache-coyote/1.1content-type:text/html;charset=iso-8859-1content-length:105date:tue, May 16:23:28 gmt
5.2. HTTP response details--Status line

Status line format: http version number status code reason description <CRLF>
Example: http/1.1 OK
The status code is used to represent the result of the server's processing of the request, which is a three-bit decimal number. The response status code is divided into 5 classes, as follows:
  

5.3, HTTP response details--Common response header

Common response headers in HTTP responses (message headers)
Location: The server goes through this header to tell the browser where to jump
Server: Servers through this header, tell the browser server model
Content-encoding: The server through this header, tells the browser, the data compression format
Content-length: The server passes this header and tells the browser the length of the loopback data
Content-language: The server uses this header to tell the browser the locale
Content-type: The server passes this header and tells the browser the type of loopback data
Refresh: The server passes this header and tells the browser to refresh periodically
Content-disposition: The server through this header, tell the browser to download the way to hit the data
Transfer-encoding: The server uses this header to tell the browser that the data is being echoed in chunks
Expires:-1 Control browser do not cache
Cache-control:no-cache
Pragma:no-cache

Six, set the response header on the server to control the behavior of the client browser 6.1, set the location response header, to achieve request redirection
Package Gacl.http.study;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * @author GaCl * */public class ServletDemo01 extends HttpServlet {    public void doget (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException {        Response.setstatus (302);//Set the response status code of the server        /**         * Set the response header, the server through the location of this header, to tell the browser where to jump, this is called the request redirect         */        Response.setheader ("Location", "/javaweb_httpprotocol_study_20140528/1.jsp");    }    public void DoPost (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException {        This.doget (request, response);}    }

When you use the URL address "http://localhost:8080/JavaWeb_HttpProtocol_Study_20140528/servlet/ServletDemo01" in the browser to access ServletDemo01 , you can see the status Code and response header information sent to the browser after the server responds, as shown in:

  

The server returns a 302 status code to tell the browser that you want the resources I have not, but I pass the location response header to tell you where there is, and the browser parses the response header location after knowing to jump to/javaweb_httpprotocol_study_ 20140528/1.jsp page, so it will automatically jump to 1.jsp, as shown in:

  

6.2, set the content-encoding response header, tell the browser data compression format
Package Gacl.http.study;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.util.zip.gzipoutputstream;import Javax.servlet.servletexception;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * @author GaCl * This applet is used to demonstrate the following two small knowledge points * 1, use Gzipoutputstream stream to compress data * 2, set the response header content-encoding to tell the browser, the server sends back the data compressed format */public class            ServletDemo02 extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) Throws Servletexception, IOException {String data = "Abcdabcdabcdabcdabcdabcdab" + "CDABCDABCDA Bcdabcdabcdabcdabcdabc "+" DABCDABCDABCDABCDABCDABCDABCDABC "+" dabcdabcdabcdabcdabcdabcdabc                 Dabcdab "+" Cdabcdabcdabcdabcdabcdabcdabcdabcdab "+" Cdabcdabcdabcdabcdabcdabcdabcdabcdab "+ "Cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "CdabcdabcdabcdabCDABCDABCDABCDABCDABCD ";                System.out.println ("The size of the original data is:" + data.getbytes (). length);        Bytearrayoutputstream bout = new Bytearrayoutputstream (); Gzipoutputstream gout = new Gzipoutputstream (bout);        Buffer Gout.write (data.getbytes ());        Gout.close ();        Get compressed data byte g[] = Bout.tobytearray ();        Response.setheader ("content-encoding", "gzip");        Response.setheader ("Content-length", G.length + "");    Response.getoutputstream (). write (g); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex    ception {this.doget (request, response); }}

The server sends a response message to the browser as follows:

The compression formats supported by the browser are:

6.3. Set the Content-type response header to specify the loopback data type
Package Gacl.http.study;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 ServletDemo03 Extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Ser          Vletexception, IOException {/** * Browser can receive (accept) data types are: * application/x-ms-application, * Image/jpeg, * application/xaml+xml, * image/gif, * image/pjpeg, * application/x-ms-x          BAP, * application/vnd.ms-excel, * application/vnd.ms-powerpoint, * application/msword, */Response.setheader ("Content-type", "image/jpeg");//Use the Content-type response header to specify that the data type sent to the browser is "image/jpeg"//Read in the project Wp_20131005_002.jpg this image in the IMG folder under the root directory, returns an input stream inputstream in = This.getservletconteXT (). getResourceAsStream ("/img/wp_20131005_002.jpg");        byte buffer[] = new byte[1024];        int len = 0; OutputStream out = Response.getoutputstream ();//Get output stream while (len = in.read (buffer)) > 0) {//Read input stream (in) contents stored in buffer Zone (buffer) out.write (buffer, 0, Len);//Output the contents of the buffer to the browser}} public void DoPost (HttpServletRequest re Quest, HttpServletResponse response) throws Servletexception, IOException {this.doget (Request, response    ); }}

The server sends a response message to the browser as follows:

The result of the ServletDemo03 operation is as follows:

A picture is displayed in the browser

6.4, set the refresh response header, let the browser refresh periodically
Package Gacl.http.study;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class ServletDemo04 extends HttpServlet {public    void doget ( HttpServletRequest request, HttpServletResponse response)            throws Servletexception, IOException {        /**         * Set the refresh response header to refresh the browser every 3 seconds         //        /Response.setheader ("Refresh", "3");        /**         * Set Refresh response header, allow the browser to jump to http://www.baidu.com         *        /Response.setheader ("Refresh", "3;url="/HTTP//in 3 seconds) Www.baidu.com ' ");        Response.getwriter (). Write ("GaCl");    }    public void DoPost (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException {        this.doget (request, response);    }}
6.5, set content-disposition response header, let the browser download files
Package Gacl.http.study;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 ServletDemo05 Extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Ser Vletexception, IOException {/** * Set content-disposition response header, let the browser download the file */Response.setheader ("        Content-disposition "," attachment;filename=xxx.jpg ");        InputStream in = This.getservletcontext (). getResourceAsStream ("/img/1.jpg");        byte buffer[] = new byte[1024];        int len = 0;        OutputStream out = Response.getoutputstream ();        while (len = in.read (buffer)) > 0) {out.write (buffer, 0, Len); }} public void DoPost (HttpServletRequest request, httpservletresponse response) throws ServletException, IOException {this.doget (request, response); }}

Accessing ServletDemo05 in the browser pops up the file download box, as shown in:

  

Here to the original author (aloof Wolf) salute, the original address: http://www.cnblogs.com/xdp-gacl/p/3751277.html

Java Basics---HTTP protocol

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.