Http协议中的方法
http://blog.csdn.net/macroway/article/details/1428541
孟巖老師將“REST”稱為一場(chǎng)Web開發(fā)思想的變革,而就我個(gè)人理解而言,REST對(duì)Mashup應(yīng)用的價(jià)值要遠(yuǎn)大于對(duì)Web應(yīng)用架構(gòu)的影響。
這里不想糾纏于REST和MashUp兩個(gè)概念,關(guān)于它們的文章我已經(jīng)寫了一些,不過(guò)還暫時(shí)不打算發(fā)在blog中。
打算介紹一下http協(xié)議中涉及的方法,在互聯(lián)網(wǎng)上搜索了很久,發(fā)現(xiàn)中文文章中深究http協(xié)議的很少,通常談到http協(xié)議也只是關(guān)注于Get和Post兩個(gè)方法,最多提一下head方法。
因?yàn)橛行枰?#xff0c;所以我就再次做一下科普。
如果可以的話,請(qǐng)進(jìn)入windows命令行,如果你使用的是非windows操作系統(tǒng)的話,我相信你肯定知道如何進(jìn)入命令行。
輸入:
telnet www.google.com 80
很快你的命令行窗口變得一團(tuán)漆黑了,而這個(gè)時(shí)候的你,已經(jīng)開始訪問(wèn)google的web了。與我們通常使用瀏覽器后的感覺(jué)不同,現(xiàn)在的你無(wú)所適從。
如果你愿意嘗試,現(xiàn)在的你輸入get或者h(yuǎn)ead等都可以獲得響應(yīng),只不過(guò)響應(yīng)的內(nèi)容讓人有些失望。
通常瀏覽器會(huì)幫助我們構(gòu)建http的協(xié)議頭信息,從而向?qū)Ψ椒?wù)器發(fā)送包,獲得響應(yīng)的http協(xié)議頭信息和正文內(nèi)容,再將正文內(nèi)容解析展示在我們面前。
如果你使用過(guò)任何sniffer工具,你都會(huì)發(fā)現(xiàn),http協(xié)議頭中通常我們的起始內(nèi)容是get或post。雖然瀏覽器能夠幫助我們只是發(fā)送head請(qǐng)求,但通常我們不會(huì)這樣去做。
http協(xié)議只有g(shù)et、post和head方法么?它還包含些什么方法呢?
關(guān)于http協(xié)議涉及方法的兩個(gè)名詞解釋:safe型方法和Idempotent型方法。
safe型方法通常是指不影響服務(wù)器端資源的行為,它的操作結(jié)果僅僅是讀取信息,GET和HEAD多數(shù)情況下被認(rèn)為具有“safe”的特性。
Idempotent型方法正如Idempotent這個(gè)詞的意思一樣,如果一個(gè)方法,n次調(diào)用與一個(gè)獨(dú)立請(qǐng)求所產(chǎn)生的資源消耗相同,即沒(méi)有邊際損耗,則我們可以認(rèn)為這類方法是idempotent型方法,get、head、put、delete、options和trace都具有這樣的特性。
http協(xié)議通常包括的方法有很多,以下我只列出我在http/1.1協(xié)議定義中看到的方法:get、post、head、options、put、delete、trace、connect。
get方法用于獲取URI資源,是最為常用的一種方法。
post方法用于向指定URI提交內(nèi)容,服務(wù)器端響應(yīng)其行為,該方法也極為常用。
head方法向URI發(fā)送請(qǐng)求,僅僅只需要獲得響應(yīng)的協(xié)議頭。
put方法用于向URI發(fā)送請(qǐng)求,若URI不存在,則要求服務(wù)器端根據(jù)請(qǐng)求創(chuàng)建資源。當(dāng)URI存在時(shí),服務(wù)器端必須接受請(qǐng)求內(nèi)容,將其作為URI資源的修改后版本。
delete方法用于刪除URI標(biāo)識(shí)的指定資源。
trace方法用于激活服務(wù)器端對(duì)請(qǐng)求的循環(huán)反饋,反饋?zhàn)鳛閔ttp響應(yīng)的正文內(nèi)容被傳輸回客戶端。
connect方法通常被用于使用代理連接。
對(duì)于上面提到的這些方法,相信大家如果肯花時(shí)間,自然可以在http協(xié)議的標(biāo)準(zhǔn)中看到更加詳細(xì)的內(nèi)容。但有一點(diǎn)是我需要說(shuō)明的,以上提到的方法,除了get、post和head可以通過(guò)瀏覽器應(yīng)用外,其它的方法例如put和delete使用起來(lái)有些問(wèn)題。但ajax的出現(xiàn)為我們提供了一種解決的方式,ajax使用xmlhttprequest進(jìn)行操作時(shí),在發(fā)送請(qǐng)求之前,可以通過(guò)設(shè)置request type的方式來(lái)完成對(duì)請(qǐng)求方法的設(shè)定。
諸位如果作為servlet開發(fā)的人可以嘗試一下直接在servlet中實(shí)現(xiàn)doPut、doDelete等方法,并在客戶端使用ajax對(duì)相同的URI使用不同的方法進(jìn)行請(qǐng)求,相信大家可以玩得愉快:)
?
==========
HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods
?
An HTTP request is a class consisting of HTTP style requests, request lines, request methods, request URL, header fields, and body content. The most common methods that are used by a client in an HTTP request are as follows:-1) GET:- Used when the client is requesting a resource on the Web server.
2) HEAD:- Used when the client is requesting some information about a resource but not requesting the resource itself.
3) POST:- Used when the client is sending information or data to the server—for example, filling out an online form (i.e. Sends a large amount of complex data to the Web Server).
4) PUT:- Used when the client is sending a replacement document or uploading a new document to the Web server under the request URL.
5) DELETE:- Used when the client is trying to delete a document from the Web server, identified by the request URL.
6) TRACE:- Used when the client is asking the available proxies or intermediate servers changing the request to announce themselves.
7) OPTIONS:- Used when the client wants to determine other available methods to retrieve or process a document on the Web server.
8) CONNECT:- Used when the client wants to establish a transparent connection to a remote host, usually to facilitate SSL-encrypted communication (HTTPS) through an HTTP proxy.
總結(jié)
以上是生活随笔為你收集整理的Http协议中的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 知己知彼 防范攻击:网络攻击步骤与黑客攻
- 下一篇: 什么是 MIME Type