Http协议中的方法
2013-12-04 15:12
190 查看
/article/1779808.html
孟岩老师将“REST”称为一场Web开发思想的变革,而就我个人理解而言,REST对Mashup应用的价值要远大于对Web应用架构的影响。
这里不想纠缠于REST和MashUp两个概念,关于它们的文章我已经写了一些,不过还暂时不打算发在blog中。
打算介绍一下http协议中涉及的方法,在互联网上搜索了很久,发现中文文章中深究http协议的很少,通常谈到http协议也只是关注于Get和Post两个方法,最多提一下head方法。
因为有需要,所以我就再次做一下科普。
如果可以的话,请进入windows命令行,如果你使用的是非windows操作系统的话,我相信你肯定知道如何进入命令行。
输入:
telnet www.google.com 80
很快你的命令行窗口变得一团漆黑了,而这个时候的你,已经开始访问google的web了。与我们通常使用浏览器后的感觉不同,现在的你无所适从。
如果你愿意尝试,现在的你输入get或者head等都可以获得响应,只不过响应的内容让人有些失望。
通常浏览器会帮助我们构建http的协议头信息,从而向对方服务器发送包,获得响应的http协议头信息和正文内容,再将正文内容解析展示在我们面前。
如果你使用过任何sniffer工具,你都会发现,http协议头中通常我们的起始内容是get或post。虽然浏览器能够帮助我们只是发送head请求,但通常我们不会这样去做。
http协议只有get、post和head方法么?它还包含些什么方法呢?
关于http协议涉及方法的两个名词解释:safe型方法和Idempotent型方法。
safe型方法通常是指不影响服务器端资源的行为,它的操作结果仅仅是读取信息,GET和HEAD多数情况下被认为具有“safe”的特性。
Idempotent型方法正如Idempotent这个词的意思一样,如果一个方法,n次调用与一个独立请求所产生的资源消耗相同,即没有边际损耗,则我们可以认为这类方法是idempotent型方法,get、head、put、delete、options和trace都具有这样的特性。
http协议通常包括的方法有很多,以下我只列出我在http/1.1协议定义中看到的方法:get、post、head、options、put、delete、trace、connect。
get方法用于获取URI资源,是最为常用的一种方法。
post方法用于向指定URI提交内容,服务器端响应其行为,该方法也极为常用。
head方法向URI发送请求,仅仅只需要获得响应的协议头。
put方法用于向URI发送请求,若URI不存在,则要求服务器端根据请求创建资源。当URI存在时,服务器端必须接受请求内容,将其作为URI资源的修改后版本。
delete方法用于删除URI标识的指定资源。
trace方法用于激活服务器端对请求的循环反馈,反馈作为http响应的正文内容被传输回客户端。
connect方法通常被用于使用代理连接。
对于上面提到的这些方法,相信大家如果肯花时间,自然可以在http协议的标准中看到更加详细的内容。但有一点是我需要说明的,以上提到的方法,除了get、post和head可以通过浏览器应用外,其它的方法例如put和delete使用起来有些问题。但ajax的出现为我们提供了一种解决的方式,ajax使用xmlhttprequest进行操作时,在发送请求之前,可以通过设置request type的方式来完成对请求方法的设定。
诸位如果作为servlet开发的人可以尝试一下直接在servlet中实现doPut、doDelete等方法,并在客户端使用ajax对相同的URI使用不同的方法进行请求,相信大家可以玩得愉快:)
==========
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.
孟岩老师将“REST”称为一场Web开发思想的变革,而就我个人理解而言,REST对Mashup应用的价值要远大于对Web应用架构的影响。
这里不想纠缠于REST和MashUp两个概念,关于它们的文章我已经写了一些,不过还暂时不打算发在blog中。
打算介绍一下http协议中涉及的方法,在互联网上搜索了很久,发现中文文章中深究http协议的很少,通常谈到http协议也只是关注于Get和Post两个方法,最多提一下head方法。
因为有需要,所以我就再次做一下科普。
如果可以的话,请进入windows命令行,如果你使用的是非windows操作系统的话,我相信你肯定知道如何进入命令行。
输入:
telnet www.google.com 80
很快你的命令行窗口变得一团漆黑了,而这个时候的你,已经开始访问google的web了。与我们通常使用浏览器后的感觉不同,现在的你无所适从。
如果你愿意尝试,现在的你输入get或者head等都可以获得响应,只不过响应的内容让人有些失望。
通常浏览器会帮助我们构建http的协议头信息,从而向对方服务器发送包,获得响应的http协议头信息和正文内容,再将正文内容解析展示在我们面前。
如果你使用过任何sniffer工具,你都会发现,http协议头中通常我们的起始内容是get或post。虽然浏览器能够帮助我们只是发送head请求,但通常我们不会这样去做。
http协议只有get、post和head方法么?它还包含些什么方法呢?
关于http协议涉及方法的两个名词解释:safe型方法和Idempotent型方法。
safe型方法通常是指不影响服务器端资源的行为,它的操作结果仅仅是读取信息,GET和HEAD多数情况下被认为具有“safe”的特性。
Idempotent型方法正如Idempotent这个词的意思一样,如果一个方法,n次调用与一个独立请求所产生的资源消耗相同,即没有边际损耗,则我们可以认为这类方法是idempotent型方法,get、head、put、delete、options和trace都具有这样的特性。
http协议通常包括的方法有很多,以下我只列出我在http/1.1协议定义中看到的方法:get、post、head、options、put、delete、trace、connect。
get方法用于获取URI资源,是最为常用的一种方法。
post方法用于向指定URI提交内容,服务器端响应其行为,该方法也极为常用。
head方法向URI发送请求,仅仅只需要获得响应的协议头。
put方法用于向URI发送请求,若URI不存在,则要求服务器端根据请求创建资源。当URI存在时,服务器端必须接受请求内容,将其作为URI资源的修改后版本。
delete方法用于删除URI标识的指定资源。
trace方法用于激活服务器端对请求的循环反馈,反馈作为http响应的正文内容被传输回客户端。
connect方法通常被用于使用代理连接。
对于上面提到的这些方法,相信大家如果肯花时间,自然可以在http协议的标准中看到更加详细的内容。但有一点是我需要说明的,以上提到的方法,除了get、post和head可以通过浏览器应用外,其它的方法例如put和delete使用起来有些问题。但ajax的出现为我们提供了一种解决的方式,ajax使用xmlhttprequest进行操作时,在发送请求之前,可以通过设置request type的方式来完成对请求方法的设定。
诸位如果作为servlet开发的人可以尝试一下直接在servlet中实现doPut、doDelete等方法,并在客户端使用ajax对相同的URI使用不同的方法进行请求,相信大家可以玩得愉快:)
==========
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.
相关文章推荐
- HTTP协议下的GET方法和POST的区别 理解
- LoadRunner本机录制http协议程序遇到的问题以及解决方法
- 利用Socket HTTP协议获得HTML代码方法
- Android编程使用HTTP协议与TCP协议实现上传文件的方法
- HTTP协议与HTTP表单传输格式,再论get、post方法
- 牛客网Java刷题知识点之什么是HTTP协议、什么是HTTP隧道、HTTP响应的结构是怎么样的、HTTP报头包含哪些、HTTP中GET与POST方法有什么区别
- 浅谈HTTP协议中请求方法以及一些常见错误
- C# 两种方法实现HTTP协议迷你服务器
- HTTP协议分析系列(三)------HTTP协议方法和状态码
- HTTP协议分析系列(三)------HTTP协议方法和状态码
- 浅析HTTP协议六种请求方法(get,head,put,delete,post)
- HTTP协议中请求方法分析
- Android编程使用HTTP协议与TCP协议实现上传文件的方法
- dubbo基于http协议嵌入现有框架配置方法。
- Android使用http协议的GET和POST方法!!!
- Android学习笔记---28_网络通信之通过HTTP协议实现文件上传,组拼http 的post方法,传输内容
- xcode升级后ios9.0不能使用http协议的版本的解决方法介绍
- 关于http协议请求报文伪造响应的方法
- HTTP协议方法以及报头分析
- SOA服务的基本实现方法—使用HTTP协议传输XML请求(POX-over-HTTP)