1 http请求
请求内容
GET /day08_web/hello HTTP/1.1 --请求行
Accept: text/html, application/xhtml+xml, */* --请求头(多个key-value对象)
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) LBBROWSER
Accept-Encoding: gzip, deflate
Host: localhost:8080
Connection: Keep-Alive
--一个空行
name=eric&password=123456 --(可选)实体内容
1.1 请求行
GET /day08_web/hello HTTP/1.1
#http协议版本
http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭
http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求
#请求资源
URL:统一资源定位符。http://localhost:8080/day08_web/index.html 只能定位互联网资源(是URI的子集)
URI:统一资源标识符。/day08_web/hello。用于标记任何资源,可以是本地文件系统,局域网的资源,也可以是互联网资源
#请求方式
常见的请求方式:GET、POST、HEAD、TRACE、PUT、CONNECT、DELETE
常用的请求方式:GET 和POST
表单提交:
<form action=”提交地址” method=”GET/POST”>
</form>
GET vs POST 区别
1)GET方式提交
a)地址栏(URL)会跟上参数数据。以?开头,多个参数之间以&分割
b)GET方式提交数据有限制,数据不超过1K
c)GET方式不能提交敏感数据,因为在地址栏可以看到
GET请求头
GET /day08_web/testMethod.html?name=qqq&password=11111 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:8080/day08_web/testMethod.html
Accept-Language: zh-CN
User-Agent : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) LBBROWSER
Accept-Encoding : gzip, deflate
Host: localhost:8080
Connection: Keep-Alive
2)POST方式提交
a)参数不会跟着URI后面,参数而是给在请求的实体内容中,没有?开头,多个
参数之间以&分割
POST请求头
POST /day08_web/testMethod.html HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:8080/day08_web/testMethod.html?name=123&password=12345
Accept-Language: zh-CN
User-Agent : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) LBBROWSER
Content-Type: application/x-www-form-urlencoded
Accept-Encoding : gzip, deflate
Host: localhost:8080
Content-Length: 21
Connection: Keep-Alive
Cache-Control: no-cache
name=123&password=12345
得到以上的两种请求方式的html文件:testMethod.html
<!DOCTYPE html>
<html>
<head>
<title>GET和POST请求测试</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h3>GET请求</h3>
<form action="testMethod.html" method="GET">
用户名:<input type="text" name="name"/></br>
密码:<input type="text" name="password"></br>
<input type="submit" value="提交"/>
</form>
<hr/>
<h3>POST请求</h3>
<form action="testMethod.html" method="POST">
用户名:<input type="text" name="name"/></br>
密码:<input type="text" name="password"></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
1.2 请求头
Accept: text/html,image/* -- 浏览器接受的数据类型 Accept-Charset: ISO-8859-1 -- 浏览器接受的编码格式 Accept-Encoding: gzip,compress --浏览器接受的数据压缩格式 Accept-Language: en-us,zh- --浏览器接受的语言 Host: www.it315.org:80 --(必须的)当前请求访问的目标地址(主机:端口) If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --浏览器最后的缓存时间 Referer: http://www.it315.org/index.jsp -- 当前请求来自于哪里 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --浏览器类型 Cookie:name=eric -- 浏览器保存的cookie信息 Connection: close/Keep-Alive -- 浏览器跟服务器连接状态。close:连接关闭 keep-alive:保存连接。 Date: Tue, 11 Jul 2000 18:23:51 GMT -- 请求发出的时间 |
1.3 实体内容
只有POST提交的参数会放到实体内容中
1.4 HttpServletRequest对象
HttpServletRequest对象作用是用于获取请求数据
核心的API:
请求行:
request.getMethod(); //请求方式
request.getRequestURI(); //请求资源
request.getRequestURL(); //请求资源
request.getProtocol(); //请求http协议版本
请求头:
request.getHeader(“名称”); //根据请求头获取请求值
request.getHeaderNames(); //获取所有的请求头名称
实体内容:
request.getInputStream(); //获取实体内容数据
具体实例:用到POST提交时要获取实体内容,需要的html文件:textMethod.html
<!DOCTYPE html>
<html>
<head>
<title>GET和POST请求测试</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h3>GET请求</h3>
<form action="testMethod.html" method="GET">
用户名:<input type="text" name="name"/></br>
密码:<input type="text" name="password"></br>
<input type="submit" value="提交"/>
</form>
<hr/>
<h3>POST请求</h3>
<form action="/day08_web/Demo_Request1" method="POST">
用户名:<input type="text" name="name"/></br>
密码:<input type="text" name="password"></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
实现的主程序为:Demo_Request1.java
package Request;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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 Demo_Request1 extends HttpServlet {
/**
*1)tomcat服务器接收到浏览器发送的请求数据,然后封装到HttpServetRequest对象
*2)tomcat服务器调用doGet方法,然后把request对象传入到servlet中
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 3)从request对象取出请求数据
*/
/*
* 3.1 请求行 格式:(GET...)
*/
// t1(request);
/*
* 3.2 请求头
*/
// t2(request);
}
//获取POST提交方式的实体内容
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 3.3 请求实体内容
*/
InputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf)) != -1) {
String str = new String(buf,0,len);
System.out.println(str);
}
}
private void t2(HttpServletRequest request) {
String host = request.getHeader("Host");//根据头名称得到偷得内容
System.out.println(host+"-------");
//遍历所有请求头
Enumeration<String> enums = request.getHeaderNames();
while(enums.hasMoreElements()) { //判断是否有下一个元素
String headerName = enums.nextElement();
String headerValue = request.getHeader(headerName);
System.out.println(headerName+": "+headerValue);
}
}
private void t1(HttpServletRequest request) {
System.out.println("请求方式:"+request.getMethod());//请求方式
System.out.println("URI:"+request.getRequestURI());//请求资源
System.out.println("URL:"+request.getRequestURL());
System.out.println("http版本协议:"+request.getProtocol());//http版本协议
}
}
响应内容格式
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 73
Date Tue, 20 Dec 2016 09:26:13 GMT