现在的位置: 首页 > 综合 > 正文

HTTP介绍

2013年10月02日 ⁄ 综合 ⁄ 共 2812字 ⁄ 字号 评论关闭

Computers on the World Wide Web use the HyperText Transfer Protocol to talk with each other.

HTTP是一个标准协议,它规定了统一的用于在互联网上交换信息时使用的信息的格式。

client端发送http请求信息到server端,server端返回client端请求的信息。

首先我们来看浏览器端(或其他客户端)发送给HTTP服务器的请求的样子。

Each client-server transaction, whether a request or a response, consists of three main parts

  1. A response or request line
  2. Header information
  3. The body

一个例子:

GET http://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: BAIDUID=55966D8CAA755B31EFE414F72932D92E:FG=1

  • 其中,GET http://www.baidu.com/ HTTP/1.1就是一个request line。 The request line from the client consists of a request method, the address of the file requested and the HTTP version number.

其通常使用的method有三种:

GET

The GET method is used to retrieve web pages from the server. It is a part of the request line from the client and tells the server to pass a copy of the document back to the browser or run a CGI program.

GET is also used to send user information from an online form on a web page to the server. Through this, the data is sent as a part of the URL in 'name-value' pairs.

例如在一个login.html中,用户在文本框中输入的自己的first name和last name就可以用GET发送到服务器端进行验证。

发送时,URL会变成:

www.sitename/cgi-bin/validate.cgi?fname=John&lname=Doe

//发送的信息和http地址中用?分割开,fname=John就是一个name-value pair,同时每一个不同pair中用$分割。

POST

Data from the POST method is sent by the client as a part of the request body. The server receives this data and sends it to a CGI program along with other variables. The METHOD attribute inside the <FORM> tag will carry POST as its value. The big advantage
in using POST over GET is that the data is not open to prying eyes. Also, via GET, you can transfer only a limited amount of data; POST exerts no such limitations.

HEAD

This method is used to find information about a document located on the server not retrieve it. It is very similar to the GET method except no data is returned by the server.

  • The header data that consists of configuration information about the client and its document viewing preferences. The header is a series of lines, each of which contains a specific detail about the client and ends with a blank line. A header
    may look like this:

前面的例子在request line之后的内容都是header information。其中一些例如cookie是可选的。

header information之后会有一个空行,表示header的结尾。

  • The body of the request will contain data sent by the client via POST method.

接下来是server response。前面已经说了,response也是三个部分。

HTTP/1.1 200 OK
Date: Sun, 20 Nov 2011 02:59:32 GMT
Server: BWS/1.0
Content-Length: 3092
Content-Type: text/html;charset=gb2312
Cache-Control: private
Expires: Sun, 20 Nov 2011 02:59:32 GMT
Content-Encoding: gzip
Connection: Keep-Alive

  • The HTTP version used is 1.1 and the status code 200 and 'OK' explain the result of the client's request. 200是最常见的返回代码,可以访问http://www.webdevelopersnotes.com/basics/http_codes.php3查看其他代码的意思。

其中的404,403也是常见错误代码。

  • The header from the server contains information about the server software and the document sent to the client.

上例表示,server端返回了一个html文件,长度为3092字节。使用字符集为gb2312.

抱歉!评论已关闭.