HTTP介绍

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.
  • 其中的"the address of the file requested" 也就是URL。URL通常是更复杂的形式,例如http://www.baidu.com/select/selectBeerTaste.jsp?color=dark&taste=malty 也就是说,可以通过URL传递参数。

2,常用的三种方法:

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

Post is more powerful request.  It's like a GET plus plus.  With POST, you can request something and at the same time send form data to the server.

在使用Get方式时,只能在URL中带入参数,但URL的长度是有限的。例如用户在一个博客网站写了一篇长文,通过http发送给服务器时,就无法通过在URL中带参数的方式传递。而上文中的form data就是指这样的数据。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是最常见的状态码,404,403是常见错误代码。可以访问状态码查看其他状态码的意思。

 

  • The header from the server contains information about the server software and the document sent to the client. 上例表示,server端返回了一个html文件,长度为3092字节。使用字符集为gb2312.

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值