Java Web HTTP protocol Summary __http protocol

Source: Internet
Author: User
Tags response code java web
HTTP protocol Summary HTTP protocol (Hyper-Text Transfer Protocol)

HTTP is a stateless, application-level protocol based on the request-response model, which is based on a TCP link (three handshake) and an ongoing link mechanism in the HTTP 1.1 version, most of which are built on the HTTP protocol.

A URL is a special type of URI (Uniform Resource Identifier) that contains enough information to find a resource.

The HTTP URL format is as follows:

    http://host[":" Port][abs_path]
Host: Represents a legitimate Internet host domain name and IP address. Port port number, or null indicates the use of the default ports 80 (the default value). Abs_path: Specifies the URI of the requested resource. If Abs_path is not given in the URL, it must be given as a "/" as the request URI, which is usually done automatically by the working browser.

When we enter www.hao123.com

http://www.hao123.com/

Hao123 represents the domain name, and entering the domain name will find the IP corresponding to the domain name in the DNS server.

The domain name uses port 80, which you do not need to enter.

Finally, because the resource address is not entered, the browser will add/end by default. the HTTP protocol features support for client/server-side mode. and the browser can interact with our server through the HTTP protocol. Simple and quick: When a client requests a service from a server, it simply transmits the request method and path. The request method often has get,head,post. Each method sets the type of customer contact with the server. Because of the simplicity of the HTTP protocol, HTTP protocol HTTP server is small in size, so the communication speed is fast. Flexibility: HTTP allows data objects of any type to be transmitted. The type being transferred is marked by Content-type. Stateless: There is no memory for transaction processing. A missing state means that if the preceding information is required for subsequent processing, it must be transferred again. On the other hand, the processing speed is faster without the need for the preceding information. No connection: When http/1.0, the request response is disconnected. http/1.1, the request does not disconnect immediately after the link, but after a period of time, if there is no subsequent request, will be disconnected. HTTP Protocol version

The HTTP protocol is divided into two versions, namely http/1.0 and http/1.1.

After the link is http/1.0, only one web resource link can be requested and only one response and request can be made, and the server will disconnect immediately after the response is complete.

After http/1.1 a link, you can request multiple Web resource links, send a request, the server responds, and the link is not immediately disconnected. Send the request again, directly for a period of time did not operate, automatic disconnect. HTTP Request

As can be seen from the diagram, the HTTP request is divided into three parts, the request line, the request header, the request body.

The request line is divided into three parts,

Request method: All Request way: POST,, OPTIONS, DELETE, TRACE, put, CONNECT common request Way has post,get. The difference between post and get:
POST encapsulates the parameters into the request body with high security levels and supports large data. Get displays parameters directly to the address bar, with low security levels and no support for large data.

Request Address: The requested resource. Protocol version: http/1.1

Request headers

                    accept:text/html,image/*    
                    accept-charset:iso-8859-1
                    accept-encoding:gzip
                    accept-language:zh-cn 
                    host:www.itcast.com:80
                    if-modified-since:tue, 18:23:51 GMT
                    referer:http://www.itcast.com/ index.jsp
                    user-agent:mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
                    connection:close/keep-alive   
                    

Request body: Get is null.post package parameter list.

You can see that there are some fields in the request header that are in the form of key-value pairs. Some fields are one-key pairs of multiple values. We can achieve some special effects by setting some values for the field. HTTP Response

Responses are similar to requests, divided into response lines, response headers, and response bodies.

There is 200 in the response line, which identifies the result of the request response.

The status code is composed of three digits, the first number defines the category of the response, and there are five possible values: 1xx: Indicates that the request has been received, continues processing 2xx: Success – Indicates that the request was successfully received, understood, accepted 3xx: redirect – the need for further action to complete the request 4XX: Client Error – Request has a syntax error or the request could not be implemented 5XX: server-side Error-Server failed to implement legitimate request

The common response code is as follows: 200 client request succeeded. 400 the client request has a syntax error and cannot be understood by the server. 401 Request unauthorized 404 request resource does not exist 500 server unexpected error 503 the server is currently unable to process client requests and may return to normal after a period of time.

In the corresponding header, like the request header, there is a key value pair. Can do something with the request header. Application of HTTP anti-theft chain

In some websites, can get other people's website link joins to own website, causes the user to browse from other website to the information of this station.


As pictured, there are two sites, respectively, for good guys and bad guys, where villains steal links from good people's websites. namely hotlinking.

In the request, there is a field Referer header information, which can be referer to determine whether the link is correct.

In Javaweb, the request from the servlet can get the header information.

    Compare
    String referer = Request.getheader ("Referer") with Referer and their URLs;
Get Browser Information

Gets the browser information through the user-agent of the request header.

        Prevent Chinese garbled
        response.setcontenttype ("Text/html;charset=utf-8");

        Gets the browser information
        String s = Request.getheader ("user-agent");

        Response.getwriter (). write (s);

mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/50.0.2661.102 safari/537.36

Get to browser information, System information. This field allows you to differentiate between the PC side and the mobile side when accessing. timed refresh of page

The Response header field Refresh is used to implement a page timing refresh.

        Response.setcontenttype ("Text/html;charset=utf-8");
        Response.getwriter (). Write ("Access to ...");
        Page 5 seconds will jump
        response.setheader ("Refresh", "5;url=http://www.baidu.com");

The page will jump to Baidu in 5 seconds. Implementing Redirection

Forwarding and redirection difference forwarding: Find the monitor to borrow money, he himself find rich monitor to borrow money. (two page stitching together, background quietly processing) Redirect: To find a monitor to borrow money, send a request, back I have no money, return status code 302, to the Deputy monitor address, and then to find rich monitor to borrow money, and sent again.

Implementation of redirection

response.setcontenttype ("Text/html;charset=utf-8");
        Response.getwriter () write ("Borrow money from the monitor ...");
        I have no money response.setstatus (302);
Tell me the address of Response.setheader ("location", "deputy Monitor Address"); 
Related Article

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.