Introduction to the HTTP protocol

Source: Internet
Author: User
Tags locale

cmdtelnet localhost 8080get/aa/1.html http/1.1host: To access the page to the IE installation HttpWatch can see the HTTP protocol what is the HTTP protocol: Hypertext Transfer Protocol, he is tcp/ An application-layer protocol for the IP protocol that defines the process of interacting data between a Web browser and a Web server. HTTP protocol is the cornerstone of Learning Javaweb Development HTTP protocol version: http/1.0 (client and server can only get one resource after establishing a connection), http/ 1.1 (Allow clients and servers to get multiple resources after establishing a connection) Web page development principles: Minimize the number of HTTP requests HTTP request format: get/aa/1.html http/1.1 (Request line:) The following are multiple request headers: accept:application /x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, Image/pjpeg, APPLICATION/X-MS-XBAP, application/ MSWord, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, */* (Tell the server which data types are supported) Accept-language: ZH-CN (locale) Accept-charset: (Client-supported encoding) 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) (tells the server, client's software environment) Accept-encoding:gzip, deflate (client supported compression format, compression pass) If-modified-since:sun, Mar 2015 01:56:15 GMT (tells the server when the resource is cached, and if the server page is not updated, use the cache directly on the client's page. Otherwise, the new HTML page will be useful: reduce server pressure if-none-match:w/"11-1425779775033" Host:localhost:8081 (the client tells the server that it wants to access the host name) Connection:keep-alive/close (keep the connection or close the connection after the request is complete) Referer: (The resource page before the request (from which resource (page) jumps to this page)) Useful: anti-theft chain (see if the first half of Referer is a resource on this site, and determine if you have permission to open the page) Cookie: (the client can bring data to the server via this header) Range: (The range header enables you to resume the breakpoint.) range:bytes=1000-2000. Transmission range from 1000-2000,range:bytes=1000-. The transmission range is 1000 bytes later data. range:bytes=1000, transmission last 1000 bytes) Date: (Current time value) public static void main (string[] args) {URL url = new UTL ("http://localhost : 8081/aa/1.txt "); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestproperty ("Range", "bytes=5-"); I Nputstream in = Conn.getinputstream (); int len;byte[] buffer = new byte[1024]; FileOutputStream out = new FileOutputStream ("C://a.txt", True) while ((len = in.read (buffer)) >1024) {out.write (buffer , 0, Len);} In.close;out.close;} HTTP response: HTTP status header: http/1.1 304 not Modified (status line: Describes the processing result of the server to the client, status code: 200 means: The response is correct. 302: Tell the server to find another address 304,307: Tell the client to go to the browser to find the cache 403: The user does not have permission to access the resource 404: The client's request is incorrect 500: Server side error) The following are multiple response headers: ( The response header of the server loopback can control how the client browser handles the server loopback file (open?). Download? )) location:http://www.it1315.org/index.jsp (this head with 302-shapedThe state code is used, telling the browser to access http://www.it1315.org/index.jsp will jump to the address under the Location page (that is, request redirection)) server:apache-coyote/1.1 (Tell the browser server type Content-encoding:gzip (the server tells the browser that the data is compressed) the server compresses the data to improve the performance of the entire server: (Faster page opening, reduced traffic) Content-length: Tells the browser the length of the loopback data CONTENT-LANGUAGE:ZH-CN tells the browser to echo the locale last-modified: the server through this field tells the browser the current resource cache time Refresh: The server through this header, Tell the browser how often to refresh the content-disposition: The server uses this header to tell the browser to open the file content-type:text/html;charset=gb2312 (server through this header, Set the type of file (text? Image? Other? ) Transfer-encoding: The server informs the browser of the transmitted format of data via this header (block transfer?) Cookie: Save Data etag:w/"11-1425779775033" (Cache related header: The server generates a unique identifier based on the content of the data, the data changes, and the identifier is changed.) (if the identifier is not changed, the browser takes the page from the cache (it can be updated (more finely than modified))) Expires: This header tells the browser how long it will cache the resource (1 and 0, which means no caching.) (The following two headers tell the browser not to cache data, history and browser type reasons (it is recommended to set these three headers to not cache when writing the program). (Cached content: images (data resources that do not change). Do not cache content: Real-time data, very large data changes cache-control:no-cachepragma:no-cacheconnection:close/keep-alive (if the corresponding end is disconnected) Accept-ranges:bytes/none (whether the server supports a Range breakpoint continuation (bytes: support, none: Not supported)) content-range:1000-3000/5000 (returns 1000-3000 bytes, Total bytes 5000) (commonly used as client program) Date:sun, 06:58:00 gmtservlet Web page data compression: By controlling Content-length and CoNtent-encoding to control the server compressed Web page data public void doget (HttpServletRequest req, HttpServletResponse Rep) throws Servletexception, ioexception{string data = "xxxxxxxx"; SYSTEM.OUT.PRINTLN ("Raw Data Size:" +data.getbytes (). length); Bytearrayoutputstream bout = new Bytearrayoutputstream (); Gzipoutputstream bout = new Gzipoutputstream (bout); Gout.write (Data.getbytes ()); Gout.close ();//close stream, write all data to bout byte [] gzip = Bout.tobytearray ();//Get compressed result System.out.println ("Compressed result is:" +gzip.length)//Notify browser data in compressed format rep.setheader (" Content-encoding "," gzip "); Rep.setheader (" Content-length ", gzip.length+" "); Rep.getoutputstream (). write (gzip); Servlet Web page Data format://Control the Web page data type by controlling the Content-type header field. public void Doget (HttpServletRequest req, HttpServletResponse Rep) throws Servletexception, Ioexception{rep.setheader ( "Content-type", "image/bmp"); InputStream in = This.getservletcontext (). getResourceAsStream ("/1.bmp"); int len = 0;byte [] buffer = new Byte[1024];outputstream out = Rep.getoutputstream (), while ((Len=in.read (buffer)) >0) {Out.write ( buffer,0, Len);}} The Servlet Control page refreshes the page every 3 seconds//By controlling the refresh to control how often the page requests to start again (commonly used in: stocks. Chat with many people. ) public void Doget (HttpServletRequest req, HttpServletResponse Rep) throws Servletexception, ioexception{// Rep.setheader ("Refresh", "3;url= ' www.baidu.com '");//3 seconds after the jump to the Baidu website (only jump once) rep.setheader ("Refresh", "3);// Jump to this page again after 3 seconds (infinite loop) String data= "Open javaweb Journey"; Rep.getoutputstream (). Write (Data.getbytes ());} Download file public void doget (HttpServletRequest req, HttpServletResponse Rep) throws Servletexception, ioexception{ Rep.setheader ("Content-disposition", "attachment;filename=3.jpg"); Inpputstream in = new This.getservletcontext (). Getrescorceasstream ("/3.jpg"), int len;byte[] buffer = new Byte[1024];ouputstream out = new Rep.getoutputstream (), while ( (len = in.read (buffer)) >0) {out.write (buffer, 0, Len);}}

Introduction to the 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.