Turn from: http://www.cnblogs.com/ok-lanyan/archive/2012/07/14/2591204.html
HTTP is an object-oriented protocol belonging to the application layer, which is suitable for distributed hypermedia information System because of its simple and fast way. It was proposed in 1990, after several years of use and development, has been continuously improved and expanded. Currently used in the WWW is the sixth edition of Http/1.0, http/1.1 is in progress, and Http-ng (Next Generation of HTTP) has been proposed.
The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and quick: When a client requests a service from a server, it simply transmits the request method and path. The request method commonly has, POST. Each method prescribes a different type of customer contact with the server. Because the HTTP protocol is simple, the HTTP server's program is small, so the communication speed is very fast.
3. Flexible: HTTP allows the transfer of any type of data object. The type being transferred is marked by Content-type.
4. No connection: The implication of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives a reply from the customer, the connection is disconnected. In this way, the transmission time can be saved.
5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capability for transaction processing. A lack of status means that if the preceding information is required for subsequent processing, it must be retransmission, which may result in an increase in the amount of data transmitted per connection. On the other hand, it responds faster when the server does not need prior information.
First, HTTP
1.1 http protocol detailed URL of the article
HTTP (Hypertext Transfer Protocol) is based on the request and response mode, stateless, Application layer protocol, often based on TCP connection, HTTP1.1 version of a continuous connection mechanism, the vast majority of web development, are built on the HTTP protocol on the Web applications.
The format of the HTTP URL, which is a special type of URI that contains enough information to find a resource, is as follows:
http://host[":" Port][abs_path]
HTTP indicates that a network resource is to be located through the HTTP protocol; Host represents a legitimate Internet host domain name or IP address; port specifies a port number, NULL, and the default port 80;abs_path specifies the URI of the requested resource; If Abs_ is not given in the URL Path, then when it is the request URI, it must be given in the form of "/", which is usually done automatically by the working browser.
eg
1, Input: www.guet.edu.cn browser automatically converted into: http://www.guet.edu.cn/
2, http:192.168.0.116:8080/index.jsp
1.2 HTTP protocol Detailed request
The HTTP request consists of three parts: the request line, the message header, the request body
1. The request line begins with a method symbol, separated by a space, followed by the requested URI and protocol version, in the following format:
Method Request-uri Http-version CRLF
Where method represents the request methods; Request-uri is a Uniform Resource identifier, http-version represents the HTTP protocol version of the request, and CRLF represents a carriage return and a newline (except for the CRLF at the end, a separate CR or LF character is not allowed).
The request method (all uppercase) has a variety of methods, which are interpreted as follows:
Get request gets the resource identified by Request-uri
Post appends new data to the resource identified by Request-uri
The head request gets the response message header of the resource identified by Request-uri
The put request server stores a resource and uses Request-uri as its identity
Delete requests that the server delete the resources identified by the Request-uri
TRACE requests that the server echo the requested information received, primarily for testing or diagnostics
CONNECT reserved for future use
Options request query server performance, or query resource-related choices and requirements
Application Examples:
Get method: When accessing a Web page in the browser's address bar, the browser takes the access method to the server to obtain resources, eg:get/form.html http/1.1 (CRLF)
The Post method requires the requested server to accept the data appended to the request and is often used to submit the form.
eg:post/reg.jsp http/(CRLF)
Accept:image/gif,image/x-xbit,... (CRLF)
...
HOST:www.guet.edu.cn (CRLF)
Content-length:22 (CRLF)
Connection:keep-alive (CRLF)
Cache-control:no-cache (CRLF)
(CRLF)//The CRLF indicates that the message header has ended before the message header
user=jeffrey&pwd=1234//This is the data submitted below
The head method is almost the same as the Get method, and the HTTP header contains information that is the same as the information that is received from the request. Using this method, you can get information about the resources identified by Request-uri without having to transfer the entire resource content. This method is often used to test the validity of a hyperlink, whether it can be accessed, and whether it has recently been updated.
2, the request header later described
3, the request body (slightly)
1.3 HTTP protocol in the answer to the detailed text
After receiving and interpreting the request message, the server returns an HTTP response message.
The HTTP response is also composed of three parts: status line, message header, response body
1, 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, accepted
3XX: Redirect-requires further action to complete the request
4XX: Client Error--Request has a syntax error or the request cannot be implemented
5XX: Server-side Error-Server failed to implement legitimate request
Common status Code, status description, Description:
OK//Client Request successful
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 field
403 Forbidden//server receives request, but refuses to provide service
404 Not Found//request resource does not exist, eg: entered the wrong URL
Internal Server error//Servers unexpected error
503 Server unavailable//servers are currently unable to process client requests and may return to normal after a period of time
eg:http/1.1 OK (CRLF)
2, the response header later described
3, the response body is the content of the resources returned by the server
1.4 HTTP protocol Detailed message header
HTTP messages consist of requests from the client to the server and responses from the server to the client. Both the request message and the response message are from the start line (for the request message, the start line is the request line, for the response message, the start line is the status line), the message header (optional), the empty line (only the CRLF row), and the message body (optional).
The HTTP message header includes a normal header, a request header, a response header, and an entity header.
Each header field is made up of the name + ":" + space + value, and the name of the message header field is case independent.
1. General Header
In the normal header, a few header fields are used for all requests and response messages, but not for the transmitted entities, only for the transmitted messages.
eg
Cache-control is used to specify a caching instruction, the cache instruction is one-way (the cached instruction appearing in the response may not appear in the request), and is independent (a message's caching instruction does not affect the caching mechanism of another message processing), and HTTP1.0 uses a similar header domain as pragma.
The cached instruction at request includes: No-cache (used to indicate that the request or response message cannot be cached), No-store, Max-age, Max-stale, Min-fresh, only-if-cached;
The caching instructions for the response include: public, Private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age, S-maxage.
Eg: in order to instruct IE browser (client) do not cache pages, server-side JSP program can be written as follows: Response.sehheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "no-cache"); function equivalent to the above code, usually both//shared
This code sets the normal header field in the Sent response message: Cache-control:no-cache
The date normal header field represents the day and time the message was generated
Connection the normal header field allows you to send the option to specify a connection. For example, specify that the connection is contiguous, or specify the "close" option to notify the server that when the response completes, the connection is closed
The
2, request header
Request header allows the client to deliver the requested additional information and the client itself information to the server side. The
Common request header
Accept request header domain is used to specify which types of information the client accepts. Eg:accept:image/gif, indicating that the client wants to accept a resource in GIF format; accept:text/html, which indicates that the client wants to accept HTML text. The
Accept-charset request header field is used to specify the character set accepted by the client. eg:accept-charset:iso-8859-1,gb2312. If you do not set this field in the request message, the default is that any character set is acceptable. , the
accept-encoding request header field is similar to Accept, but it is used to specify an acceptable content encoding. Eg:accept-encoding:gzip.deflate. If the domain server is not set in the request message, the client is assumed to be acceptable for various content encodings. The
Accept-language request header field is similar to Accept, but it is used to specify a natural language. EG:ACCEPT-LANGUAGE:ZH-CN. If the header domain is not set in the request message, the server assumes that the client is acceptable to all languages. The
authorization request header domain is primarily used to prove that the client has permission to view a resource. When a browser accesses a page, if the response code received from the server is 401 (not authorized), you can send a request containing the authorization request header domain to require the server to authenticate it. The
host request header domain is primarily used to specify the Internet host and port number of the requested resource, which is usually extracted from the HTTP URL and is required when sending the request, eg: we enter in the browser: http://www.guet.edu.cn/ Index.html
The request message sent by the browser contains the Host request header domain, as follows: Host:www.guet.edu.cn
This uses the default port number of 80, and if the port number is specified, it becomes: Host:www.guet.edu.cn: Specify the port number
User-agent when we go online to the forum, we often see some welcome information, which lists the name and version of your operating system, the name and version of the browser you are using, which often makes a lot of people feel amazing, actually, The server application obtains this information from the User-agent request header domain. The User-agent request header domain allows the client to tell the server about its operating system, browser, and other properties. However, this header domain is not required, if we write a browser ourselves, do not use the User-agent request header domain, then the server side can not know our information.
Request Header Example:
get/form.html http/1.1 (CRLF)
accept:image/gif,image/x-xbitmap,image/jpeg,application/ x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/* (CRLF)
ACCEPT-LANGUAGE:ZH-CN (CRLF)
Accept-encoding:gzip,deflate (CRLF)
if-modified-since:wed,05 2007 11:21:25 GMT (CRLF)
if-none-match:w/"80b1a4c018f3c41:8317" (CRLF)
user-agent:mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.0) (CRLF)
Host:www.guet.edu.cn (CRLF)
Connection:keep-alive (CRLF)
(CRLF)
The
3, response Header
Response header allows the server to pass additional response information that cannot be placed in the status row, as well as information about the server and next access to the resources identified by Request-uri. The
commonly used response Header
Location response header field is used to redirect the recipient to a new location. The location response header domain is often used when changing a domain name. The
Server response header field contains the software information that the server uses to process the request. Corresponds to the user-agent request header domain. The following is an example of the Server response header domain: The server:apache-coyote/1.1
www-authenticate response Header field must be contained in a 401 (unauthorized) response message and the client receives a 401 response message The server-side response header contains the header domain when the authorization header domain is sent for authentication.
Eg:www-authenticate:basic realm= "Basic Auth test!" //You can see that the server is using a Basic authentication mechanism for requesting resources.
The
4, Entity header
Request and response messages can transmit an entity. An entity consists of an Entity header field and an entity body, but does not mean that the entity header field and the entity body are sent together, and that only the Entity header field can be sent. The entity header defines meta information about the entity body (eg: there is no entity body) and the resource identified by the request. The
Common entity header
Content-encoding entity header field is used as a modifier of the media type, and its value indicates the encoding of the additional content that has been applied to the entity body, so that the media type referenced in the Content-type header domain is obtained. The corresponding decoding mechanism must be used. Content-encoding the compression method used to document documents, the Eg:content-encoding:gzip
Content-language entity header field describes the natural language used by the resource. Without setting this field, the entity content is considered to be available to all language reading
. The Eg:content-language:da
Content-length entity header field is used to indicate the length of the entity body and decimal digits stored in bytes. The
Content-type Entity header Field term indicates the media type that is sent to the recipient's entity body. Eg:
content-type:text/html;charset=iso-8859-1
content-type:text/html;charset=gb2312
The Last-modified Entity header field is used to indicate the date and time the resource was last modified. The
Expires Entity header field gives the date and time the response expires. In order for the proxy server or browser to update the cache after a period of time (accessing the visited pages directly from the cache, shortening the response time and reducing the server load), we can use the Expires Entity header field to specify when the page expires. eg:expires:thu,15 SEP 2006 16:23:12 GMT
HTTP1.1 clients and caches must treat other illegal date formats (including 0) as expired. Eg: in order for the browser to not cache the page, we can also use the Expires entity header domain, set to 0,jsp in the following: Response.setdateheader ("Expires", "0");
Author: Jeffrey from: http://blog.csdn.net/gueter/article/details/1524447
Tool class for Httputil.java View Code
public class Httputil {public static string HttpGet (String httpurl) {string result = ' ";
Defaulthttpclient httpclient = new Defaulthttpclient ();//Create HTTP client httpget HttpGet = new HttpGet (Httpurl);
HttpResponse response = null; Httpparams params = Httpclient.getparams ();
Compute the network timeout with httpconnectionparams.setconnectiontimeout (params, 15 * 1000);
Httpconnectionparams.setsotimeout (Params, 20 * 1000);
try {response = Httpclient.execute (HttpGet);
httpentity entity = response.getentity ()//Get HTTP Content response.getstatusline (). Getstatuscode ()//Get HTTP status return value result = Entityutils.tostring (response.getentity ());//Get a specific return value, typically an XML file Entity.consumecontent (); If entity is not empty, the memory space is freed Httpclient.getcookiestore ();//Get Cookis Httpclient.getconnectionmanager (). s Hutdown ()//close HTTP client} catch (Clientprotocolexception e) {E. Printstacktrace ();
catch (IOException e) {e.printstacktrace ();
return result;
public static string HttpPost (string httpurl, String data) {string result = ' ";
Defaulthttpclient httpclient = new Defaulthttpclient ();
HttpPost HttpPost = new HttpPost (Httpurl);
Httpclient.setcookiestore (Datadefine.mcookiestore); Httpparams params = Httpclient.getparams ();
Compute the network timeout with httpconnectionparams.setconnectiontimeout (params, 15 * 1000);
Httpconnectionparams.setsotimeout (Params, 20 * 1000);
Httppost.setheader ("Content-type", "Text/xml");
Stringentity httppostentity; try {httppostentity = new stringentity (data, "UTF-8"