"Android Network Development" (i) HTTP request messages and HTTP response messages

Source: Internet
Author: User
Tags set cookie browser cache silverlight

HTTP messages are text-oriented, and each field in a message is an ASCII string, and the length of each field is indeterminate. HTTP has two kinds of messages: Request message and Response message.

HTTP Request message

An HTTP request message consists of 4 parts of the request line, the request header (header), the blank line and the request data, and the following figure gives the general format of the request message.

Or







[

1. Request Headers

The request line consists of 3 fields in the Request Method field, the URL field, and the HTTP protocol version field, separated by a space. For example, get/index.html http/1.1.

The HTTP protocol request method has get, POST, head, put, DELETE, OPTIONS, TRACE, CONNECT.

Some of the most common are as follows:

1). get

The most common type of request is when a client reads a document from a server, clicks on a link on a Web page, or browses through the Web page by entering the URL in the browser's address bar, using the Get method. The Get method requires the server to place the URL-positioned resource in the data section of the response message and send it back to the client. When using the Get method, the request parameters and corresponding values are appended to the URL, using a question mark ("?"). Represents the end of the URL and the start of the request parameter, and the length of the pass parameter is restricted. For example,/index.jsp?id=100&op=bind, so that data passed through get is represented directly in the address, so we can send the result of the request as a link to a friend. To use Google search Domety for example, the request format is as follows:

get/search?hl=zh-cn&source=hp&q=domety&aq=f&oq= http/1.1  
accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, 
Application/msword, Application/x-silverlight, Application/x-shockwave-flash, */*  
Referer: <a href= "http://www.google.cn/" > http://www.google.cn/</a>  
ACCEPT-LANGUAGE:ZH-CN  
accept-encoding:gzip, deflate  
user-agent: mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1. NET CLR 2.0.50727; TheWorld)  
Host: <a href= "http://www.google.cn" >www.google.cn</a>  
connection:keep-alive  
cookie:pref=id=80a06da87be9ae3c:u=f7167333e2c3b714:nw=1:tm=1261551909:lm=1261551917:s=ybycq2wpfefs4v9g; 
nid=31=ojj8d-iygaetsxlgajmqsjvhcspkvijrb6omjamnrsm8lzhky_ymfo2m4qmrkch1g0iqv9u-2hfbw7bufwvh7pgarub0rnhcju37y-
Fxlrugatx63jlv7cwmd6ub_o_r  

As you can see, requests for Get methods generally do not contain the "Request Content" section, and the request data is represented in the form of an address on the request line. The address link is as follows:

<a href= "http://www.google.cn/search?hl=zh-CN&source=hp&q=domety&aq=f&oq=" >http:// Www.google.cn/search?hl=zh-CN&source=hp

In the address "?" The next part is the request data that is sent through get, and we can see clearly in the address bar that the data is separated by the "&" symbol. Obviously, this is not a good way to send private data. In addition, because different browsers address the character limit is also different, generally can only recognize 1024 characters, so if you need to transfer large amounts of data, it is not appropriate to use the Get method.

2). POST

For situations where the above is not appropriate for a GET method, consider using post, because the Post method allows the client to provide more information to the server. The Post method encapsulates the request parameters in the HTTP request data in the form of a name/value that transmits a large amount of data so that the post mode has no limit on the size of the data being transferred and is not displayed in the URL. Also take the search domety above as an example, if the Post method is used, the format is as follows:

post/search http/1.1 accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, Application/x-silverlight,  
Application/x-shockwave-flash, */* Referer: <a href= "http://www.google.cn/" >http://www.google.cn/</a> ACCEPT-LANGUAGE:ZH-CN Accept-encoding:gzip, deflate user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1. NET CLR 2.0.50727; TheWorld) Host: <a href= "http://www.google.cn" >www.google.cn</a> connection:keep-alive cookie:pref=id= 
80a06da87be9ae3c:u=f7167333e2c3b714:nw=1:tm=1261551909:lm=1261551917:s=ybycq2wpfefs4v9g;
nid=31=ojj8d-iygaetsxlgajmqsjvhcspkvijrb6omjamnrsm8lzhky_ymfo2m4qmrkch1g0iqv9u-2hfbw7bufwvh7pgarub0rnhcju37y- Fxlrugatx63jlv7cwmd6ub_o_r hl=zh-cn&source=hp&q=domety 

As you can see, the Post method request line does not contain a data string, which is stored in the "Request Content" section and is separated by the "&" symbol between the data. Most post methods are used in a page's form. Because the post can also complete the function of get, so most people in the design of the form when all use post way, in fact, this is a misunderstanding. Get mode also has its own characteristics and advantages, we should choose whether to use Get or post with different circumstances.

3). Head

The head is like get, except that when the server receives a header request, it returns only the response headers and does not send the response content. When we only need to look at the state of a page, it is very efficient to use the head, because the content of the page is omitted during the transfer.

2. Request Head

The request header is composed of a keyword/value pair, a pair of each row, and the keywords and values are separated by the English colon ":". The request header notifies the server that there is information about the client request, and the typical request headers are:

User-agent: The browser type that generated the request.

Accept: List of content types that clients can recognize.

Host: The requested hostname allows multiple domain names to be in the same IP address as the virtual host.

3. Blank line

After the last request header is a blank line, send a carriage return and a newline character to notify the server that there is no longer a request header.

4. Request Data

The request data is not used in the Get method, but is used in the Post method. The Post method is useful for situations where a customer needs to fill out a form. The most frequently used request headers associated with requesting data are Content-type and content-length.

HTTP Message

The HTTP response is also composed of three parts: the status line, the message header, and the response body.

As shown below, the format of the HTTP response is very similar to the format of the request:







[ ]

As you can see, the only real difference in response is that the first line replaces the request information with state information. The status line indicates the requested resource condition by providing a status code.

The status line format is as follows:

Http-version Status-code reason-phrase CRLF

Where http-version represents the version of the server HTTP protocol, Status-code represents the response status code sent back by the server, and reason-phrase a textual description of the status code. The status code consists of three digits, the first number defines the category of the response, and there are five possible values. 1XX: Indicates information--indicates that the request has been received and continues processing. 2XX: Success-Indicates that the request has been successfully received, understood, and accepted. 3XX: Redirect-further action is required to complete the request. 4XX: Client Error-The request has a syntax error or the request cannot be implemented. 5XX: Server-side error-the server failed to implement a legitimate request.

Common status codes, status descriptions are described below. OK: Client request succeeded. Bad Request: Client requests have syntax errors that cannot be understood by the server. 401 Unauthorized: Request unauthorized, this status code must be used in conjunction with the Www-authenticate header domain. 403 Forbidden: The server received the request, but refused to provide the service. 404 Not Found: The request resource does not exist, for example: The wrong URL was entered. Internal Server error: Unexpected errors occurred on the servers. 503 Server Unavailable: The server is currently unable to process client requests and may return to normal after a period of time, for example: http/1.1 OK (CRLF).

An example of an HTTP response message is given below

http/1.1 OK
Date:sat, Dec 23:59:59 GMT
content-type:text/html;charset=iso-8859-1
content-length:122    Wrox homepage   !--Body Goes here-->  

Entity Part

The third part of HTTP is the optional entity body part, and the body of the entity is the load of the HTTP message. is the content of HTTP to be transferred.

HTTP messages can host many types of digital data, pictures, videos, HTML documents, software applications, credit card transactions, e-mail, and so on.

Under the actual combat, we use the browser to open the Baidu Home page, the HTTP message to combat analysis under:

Open the request message of Baidu:

get/http/1.1//Request method for Get,http protocol 1.1 Host:www.baidu.com for WW W.baidu.com user-agent:mozilla/5.0 (Windows NT 6.1; rv:19.0) gecko/20100101 firefox/19.0//user agent, which is the browser, displays the details of the browser Ac The type of file the cept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8//server can send text/html means an HTML text document type, The following documents to accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3//server can send the language zh-cn for Chinese, the following documents to Accept-encodin G:gzip, deflate//server can send the encoding format is gzip, encoding format does not conform to the browser will not explain cookie:baiduid=af6c346b14e94898933e5f858c 63f889:fg=1; bdrefer=%7burl%3a%22http%3a//news.baidu.com/%22%2cword%3a%22%22%7d;
h_ps_pssid=2097_1464_2133_1944_1788//cookie, the server stores the information on the client, and each request sends the server to the server with a cookie stored on the client. Connection:keep-alive//connection, Keep-alive hold status cache-control:max-age=0///With message transfer cache indicating Cache-control max-age>0 direct from tour When extracting max-age<=0 from the browser cache, send an HTTP request acknowledgement to the server, whether the resource has been modified or not, returns 200, and returns 304. 

Open the response message of Baidu:

http/1.1 OK        //http version 1.1    status Code 200 reason phrase OK
date:tue, APR 2013 04:27:50 GMT    //Response time Date
server:bws/ 1.0/        /server application software name and version bws/1.0
content-length:4271    //Response body content length is 4,271 bytes
content-type:text/html; Charset=utf-8    //response type is HTML text, encoding type is utf-8
cache-control:private        //Cache indicator
Expires:tue, APR 2013 04:27:50 GMT    //entity is not valid, to obtain the date and time of this entity again from the original source
Content-encoding:gzip        //The encoding for the principal is gzip
set-cookie:h_ps_pssid=2097_1464_2133_1944_1788; path=/; Domain=.baidu.com    //Set Cookie,path,domain are cookie information (scope, etc.)
connection:keep-alive    //status for stay connected

Response on the Cow B, is the source of the page:

<! DOCTYPE html><!--STATUS ok-->    

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.