HTTP Protocol 3

Source: Internet
Author: User

1 What is the HTTP protocol

HTTP (Hypertext Transfer Protocol), the Hypertext Transfer Protocol, the most widely used protocol on the Internet before 17, after which all sites start using the HTTPS protocol (based on the HTTP protocol), before learning the HTTPS protocol, you have to know the HTTP How the protocol works. The HTTP protocol is a stateless, application-level protocol based on the request-and-response pattern, which is based on the TCP link (three-time handshake), which provides a mechanism for continuous linking in the HTTP1.1 version, and most web development is now built on the HTTP protocol.
①http protocol full name is Hypertext Transfer Protocol
② based on TCP/IP
③http protocol version, 1.0 (short connection), 1.1 (long connection), current generic version is 1.1

Main Features:

The ① supports client/server-side mode. The browser can interact with the server over the HTTP protocol

② is simple and fast: When a customer wants a server-side request service, it only needs to transfer the request method and path. The request method usually has get,post,head, each of which specifies the type of client contact with the server. Because the HTTP protocol is simple, the HTTP server is small and the communication speed is fast.

③ Flexible: HTTP allows the transfer of any type of data object. The type of transmission is marked by Content-type

④ stateless: There is no memory capability for transactional processing, and a lack of state means that if the previous information is required for subsequent processing, it must be re-routed. But if you don't need the confidence of the front, the delivery speed will be fast

⑤ no connection: When HTTP1.0, the request is disconnected after the response. After HTTP1.1, the request will not be disconnected immediately but after a period of time, if no subsequent request will be broken

2 URL

Learn the HTTP protocol also need to understand the basic concept of the URL, HTTP URL (Uniform Resource Locator), is the daily visit to our site to enter the full name of the URL, the standard format is as follows:

http://host[:p Ort][abs_path]

①host indicates the host domain name or IP address

②port represents the port number, when it does not indicate the default default port of 80

③abs_path: Specifies the URI of the requested resource

When we enter www.baidu.com in the browser, the last Baidu will display the full URL format

https://www.baidu.com/

Baidu indicates the domain name , the browser will find the IP address of the domain name in the DNS server

There is no port number in the URL, indicating that the domain name is using the default port number 80

Without an input URI, the browser will default to add/end

3 HTTP Requests

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 request headers;

③ request entity content;

A full HTTP request

1 get/usermanager/test.html http/1.12  Accept: application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, image/pjpeg ... 3  accept-language: zh-cn4  user-agent: mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2; ......5  accept-encoding: gzip, deflate6  if-modified-since: Tue, 09:57:23 GMT7  If-none-match: w/"480-1514282243853"8  Host: localhost:80809Connection:Keep-alive

① Request Line:

Get/usermanager/test.html http/1.1

A. Request Method : POST,, OPTIONS, DELETE, TRACE, PUT, connect, etc. The common request method is post and get. The differences are as follows:

POST encapsulates parameters into the request body with high security levels, supporting big data

GET displays parameters directly to the address bar with low security levels and no big data support

B. Request address: A resource for the requesting server,/usermanager/test.html

C. Protocol version: http/1.1

② Request Header:

Accept:  accept-language:user-agent:accept-encoding:if-modified-since:   if-none-match:Host: localhost:80809 Connection: keep-alive 

Accept: [Tell service, I can accept file type]

Referer: [Tell the server where I come from, the message header is often used to prevent theft of the connection]

Accept-language: [Accepted character encoding]

User-agent: [Tell the server browser what the kernel is]

Accept-encoding:gzip, deflate [accept gzip, deflate compress data]

If-modified-since:tue, 09:57:23 GMT [tell the server my cache has this file, the file time is]

If-none-match:

Host: [hostname accessed]

Connection: [whether to remain connected]

4 HTTP Response

A response is similar to a request, divided into a response line, a response header, a response body

A full HTTP response

1 http/1.1 302 Moved temporarily response line2  Date: Wed, 14:57:34 GMT Bold is the response head, followed by the response head corresponding response body 3 content-type:text/html4 content-length:2155 connection:keep-alive6 set-cookie:baiduid=4d949610a08d7f289adf189603720597:fg=1; "Cookie Information"7 Set-cookie:expires=thu, 31-dec-37 23:55:55 GMT;8 set-cookie:max-age=2147483647, path=/, domain=.baidu.com9 set-cookie:bd_last_qid=11558213965171471972;Ten p3p:cp= "OTI DSP COR IVA our IND COM" One location:https://www.baidu.com/[let browser relocate to URL] A server:bws/1.1 [Tell browser Web server] - x-ua-compatible:ie=edge,chrome=1 - refresh:1;url=http://www.baidu.com [Set browser how many seconds to jump to a page] the Content-diposition:attachment;filename=2.png - transfer-encoding:chunked [encoding of transmission] - expires:-1 [tells the browser how to cache data] - Cache-control:no-cache [tells the browser how to cache data] +Pragma:no-cache [tells the browser how to cache data]

HTTP Common Response Status Description

200: No error occurred in request and response process

302: When requesting a resource, the server returns 302 indicating that the browser is moving to another resource

404: Resource Not Found

500: Server side out problem (Internal server Error)

Case 1: Download pictures

The client downloads a picture from the server side via an HTTP request

1        //get the full path to the file you want to download2String Path = This. Getservletcontext (). Getrealpath ("Images/2.png");3         4         //Get file name5String filename= path.substring (path.lastindexof ("\ \") +1);6         //the file name needs to be transcoded7String encodefilename= urlencoder.encode (filename, "UTF-8");8Response.setheader ("Content-disposition", "attachment;filename=" +encodefilename);9         Ten         //Create a file input stream OneFileInputStream FIS =NewFileInputStream (path); A         //make a buffer byte array -         byteBuff[] =New byte[1024]; -         //How many bytes are actually read each time the         intLen=0; -         //creating an output stream object -OutputStream OS =Response.getoutputstream (); -          while(Len=fis.read (Buff)) >0) +         { -Os.write (buff,0, Len); +         } A          at         //Close File Stream -         if(fis!=NULL) -         { - fis.close (); -         } -  in         if(os!=NULL) -         { to os.close (); +}

  

Case 2: Do not let the client browser cache the page, because sometimes users are accustomed to use the enter key to fetch the page, will default to fetch data from the cache, causing the client page update is not timely

To set the page to not cache before the servlet starts, add the following code:

1 // Specifies that the page does not cache 2 response.setdateheader ("Expires",-1); 3 response.setheader ("Cache-control", "No-cache"); 4 response.setheader ("Pragma", "No-cache");

Or specify the client's webpage to expire within a certain period of time

1 2 // specifies that the page cache fails for one hours 3 response.setdateheader ("Expires", System.currenttimemillis () +3600*1000);

  

HTTP Protocol 3

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.