HTTP 方法
HTTP协议 所有的方法
方法 | 说明 | 支持的HTTP协议版本 |
---|---|---|
GET | 获得资源 | 1.0、 1.1 |
POST | 传输实体主体 | 1.0、 1.1 |
PUT | 传输文件 | 1.0、 1.1 |
DELETE | 删除文件 | 1.0、 1.1 |
HEAD | 获得HTTP协议首部 | 1.0、 1.1 |
OPTIONS | 询问HTTP服务器支持的HTTP协议的方法 | 1.1 |
TRACE | 追踪路径 | 1.1 |
CONNECT | 要求用隧道协议连接代理 | 1.1 |
LINK | 建立和资源之间的关系 | 1.0 |
UNLINK | 断开连接关系 | 1.0 |
下面我们通过tomcat所支持的HTTP方法来详解每个方法。
tomcat默认支持 GET、POST、HEAD、OPTIONS方法,而不支持PUT、DELETE、TRACE方法。
下面我们配置tomcat,让tomcat支持PUT、DELETE、TRACE方法。
配置Tomcat
配置tomcat支持 PUT、DELETE方法
打开tomcat/conf/web.xml配置文件
![](https://img.haomeiwen.com/i2843224/807a4ff8cd396f0e.png)
从说明中可以看出,如果要支持PUT、DELETE方法,需要将readonly设置为true。
![](http://upload-images.jianshu.io/upload_images/2843224-74637c0d585cf86a.png)
在DefaultServlet的初始化参数中配置readonly=true。
配置tomcat支持TRACE方法
打开tomcat/conf/server.xml配置文件
![](http://upload-images.jianshu.io/upload_images/2843224-83ad82a6a1d877f1.png)
在连接器中添加 allowTrace=true, 使tomcat支持TRACE方法。
Servlet 实现 Http 方法
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletTest() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("<html><body>Http Method : GET</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("<html><body>Http Method : POST<br>")
.append("name : ").append(request.getParameter("name")).append("</body></html>");
}
@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("<html><body>Http Method : PUT</body></html>");
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("<html><body>Http Method : DELETE</body></html>");
}
@Override
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("TANCE "+request.getRequestURI()+" HTTP/1.1\r\n");
Enumeration<String> enumer = request.getHeaderNames();
while (enumer.hasMoreElements()) {
String name = enumer.nextElement();
response.getWriter().append(name+":"+request.getHeader(name)+"\r\n");
}
}
}
通过下面代码,使用不同的Method进行请求http服务。
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Options {
public static void main(String[] args) {
try {
args = new String[]{"http://localhost:9999/webDemo/test"};
URL u = new URL(args[0]);
HttpURLConnection http = (HttpURLConnection) u.openConnection();
//GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE
http.setRequestMethod("GET");
//POST方法需要使用OutputStream提交参数
// http.setDoOutput(true);
// Writer w = new OutputStreamWriter(http.getOutputStream());
// w.write("name=jijs");
// w.flush();
// w.close();
Map<String, List<String>> headers = http.getHeaderFields();
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
if(header.getKey()!=null){
System.out.println(header.getKey() + ": " + join(header.getValue()));
}else{
System.out.println(join(header.getValue()));
}
}
} catch (MalformedURLException ex) {
System.err.println(args[0] + " is not a parseable URL");
} catch (IOException ex) {
System.err.println(ex);
}
System.out.println();
}
private static String join(List<String> list) {
StringBuilder builder = new StringBuilder();
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
builder.append(iterator.next());
if (iterator.hasNext())
builder.append(", ");
}
return builder.toString();
}
}
HTTP 协议格式
Http协议格式分为客户端请求格式和服务端响应格式。
客户端请求:
- 请求行 (Http方法 空格 请求路径 空格 协议版本 \r\n)
- 请求首部 (key : value 可以多个以回车换行结束)
- 请求实体 (与请求首部中间有一空行, 请求的实体内容)
![](http://upload-images.jianshu.io/upload_images/2843224-897adee3124f313f.png)
服务器端响应
- 响应行 (协议版本 空格 状态码 空格 状态码说明)
- 响应首部 (key : value 可以多个以回车换行结束)
- 响应实体 (与请求首部中间有一空行,响应的内容)
![](http://upload-images.jianshu.io/upload_images/2843224-ac8414a86dfdfa07.png)
通过抓包分析7种方法
通过上面代码示例,请求http的7种方法,然后使用抓包工具查看7中HTTP请求,每种HTTP协议发送的内容如下:
GET方法
GET方法用来请求访问已被URI识别的资源。指定的资源经服务器解析后返回的内容。
请求信息:
GET /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 43
Date: Thu, 03 Aug 2017 10:02:00 GMT
<html><body>Http Method : GET</body></html>
POST方法
POST方法用来传输实体的主体。提交表单的信息存放在请求实体中。例如下面的例子name=jijs存放在请求实体中。
请求信息:
POST /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-type: application/x-www-form-urlencoded
Content-Length: 9
name=jijs
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 59
Date: Thu, 03 Aug 2017 10:50:31 GMT
<html><body>Http Method : POST<br>name : jijs</body></html>
PUT方法
PUT 方法用来传输文件的。就想FTP协议的文件上传一样,要求在请求报文的主体中包含文件内容,然后保存到请求URL指定的位置。
HTTP1.1的PUT方法自身不带验证机制,任何人都可能上传文件,存在安全问题,因此一般的Web网站都不使用该方法。如果使用REST,一般就会使用该方法。
请求信息:
PUT /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 43
Date: Thu, 03 Aug 2017 10:51:24 GMT
<html><body>Http Method : PUT</body></html>
DELETE方法
DELETE 方法用来删除一个文档,与PUT方法相反。DELETE方法也存在PUT存在问题,没有自带安全机制,如果采用REST,一般会使用该方法。
请求信息:
DELETE /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 46
Date: Thu, 03 Aug 2017 14:56:30 GMT
<html><body>Http Method : DELETE</body></html>
OPTIONS方法
OPTIONS 方法用来询问HTTP服务器支持那些方法。
请求信息:
OPTIONS /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
Content-Length: 0
Date: Thu, 03 Aug 2017 09:07:18 GMT
TRACE方法
TRACE方法是让Web服务器端将之前的请求通信(服务器接收到的请求头)返回给客户端的方法。
通过TRACE方法,我们就可以知道用户访问到服务器之间是否有人(代理服务器 等)修改过HTTP请求头信息。
请求信息:
TRACE /webDemo/test HTTP/1.1
User-Agent: Java/1.8.0_77
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
响应信息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 161
Date: Thu, 03 Aug 2017 10:38:43 GMT
TANCE /webDemo/test HTTP/1.1
user-agent:Java/1.8.0_77
host:localhost:9999
accept:text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
connection:keep-alive
想了解更多精彩内容请关注我的公众号
![](http://upload-images.jianshu.io/upload_images/2843224-af50fe51e979ebd0.png)