HTTP协议超级详解

来源:互联网 发布:cad不能访问到网络锁 编辑:程序博客网 时间:2024/06/06 00:21

HTTP协议

超文本传输协议HTTP(Hyper Text Transport Protocol)    W3C组织制定的。HTTP协议默认监听80端口

1、请求行:位于信息的第一行
GET /App1/2.html HTTP/1.1
GET:请求方式(method)。是默认的请求方式。常用的还有POST(引入表单) 等。
POST:通过html中的form标签中的属性method指定。
GET /App1/3.html?username=chenlili&password=123 HTTP/1.1
1、提交的数据出现在请求行中。(消息行有长度限制;数据太不安全)
2、 /App1/2.html:访问的资源的URI(Union Resource Indentity)。
3、http://localhost:8080/App1/login.html:URL(Union Resource Location)协议+主机地址+资源地
HTTP/1.1:客户端使用的协议

2、请求消息头:从第二行开始,至第一个空行结束。
Accept: text/html, application/xhtml+xml, /*
浏览器可接受的MIME类型.(文件系统中采用扩展名区分不同的文件的。网络上是用MIME类型来区分
不同的数据、有一定的对应关系(Tomcat\conf\web.xml有)
MIME类型:大类型/具体类型.例如jpg:image/jpeg或者bmp: image/bmp
Accept-Charset:浏览器通过这个头告诉服务器,它支持哪种字符集
*Accept-Encoding:gzip, deflate
浏览器能够进行解码的数据编码方式,比如gzip
Accept-Language:zh-Hans-CN,zh-Hans;q=0.5
浏览器所希望的语言种类,当服务器能够提供一种以上的语言版本时要用到。 可以在浏览器中进行设置。
Host:localhost:8080
初始URL中的主机和端口
*Referer:www.baidu.com
包含一个URL,取值为当前页面之前的那个页面地址
应用:1.看广告投放效果, 2.防盗链
*Content-Type:application/x-www-form-urlencoded; charset=UTF-8
请求正文内容类型。目前:只有通过表单提交数据,且请求方式是post时,才会出现请求正文内容,才会有该头。
默认值是:application/x-www-form-urlencoded
对应的是
常用可选值:multipart/form-data
If-Modified-Since: Wed, 02 Feb 2011 12:04:56 GMT
利用这个头与服务器的文件进行比对,如果一致,则从缓存中直接读取文件。
User-Agent:浏览器类型.
Content-Length:表示请求正文的长度 ,数据字节长度。
Connection:表示是否需要持久连接。如果服务器看到这里的值为“Keep -Alive”,
或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接)
*****Cookie:这是最重要的请求头信息之一 (会话管理)
Date:Date: Mon, 22 Aug 2011 01:55:39 GMT请求时间GMT

3、请求正文:第一个空行之后的所有内容。
只有通过表单,且请求方式是POST时才能看到正文。
当Content-Type是:application/x-www-form-urlencoded,表单提交的数据表现形式为
username=chenlili&password=321 username和password表单中输入域的name
chenlili和321就是用户输入的数据 多个输入域用&进行分割的。

二、响应部分(服务器向客户端发送的数据)
分为三部分:
1、响应行:位于信息的第一行 HTTP/1.1 200 OK
HTTP/1.1:服务器使用的协议
200:响应码。(预留了500个,目前真正有意义的也就30个左右)常用响应码:
200:一切正常
302/307:临时重定向
304:未修改(缓存)
404:资源不存在
500:服务器内部错误(自己的程序出错了)。
OK:对错误的描述信息。(OK表示一切正常)

2、响应消息头:从第二行开始,至第一个空行结束。
*Location: http://www.it315.org/index.jsp指示新的资源的位置
302+该头:完成是请求的重定向。

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setStatus(302); //临时重定向响应码
response.setHeader( “Location” , “/day03_00_ResponseHeader/servlet/ResponseHeaderDemo2” );
Server:apache tomcat指示服务器的类型
*Content-Encoding:告知客户端服务器发送的数据采用的编码类型gzip(压缩格式)
//把数据压缩后打给浏览器:节省带宽,增强用户体验
public class ResponseHeaderDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = “aaaaaa” ;
//GZIP压缩
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //内存输出流
GZIPOutputStream gout = new GZIPOutputStream(baos);
gout.write(data.getBytes()); //输出—压缩—》 baos 中
gout.close();
byte b[] = baos.toByteArray(); //压缩后的数据
//通知客户端采用编码方式
response.setHeader( “Content-Encoding” , “gzip” );
response.setHeader( “Content-Length” , b. length + “” );
response.getOutputStream().write(b); //把数据打给浏览器
*Content-Length:80 告诉浏览器正文的长度
Content-Language:zh-cn服务发送的文本的语言
*Content-Type:text/html默认的; 告知客户端请求正文的MIME类型
Last-Modified:Tue, 11 Jul 2000 18:23:51 GMT文件的最后修改时间
*Refresh: 1;url=http://www.it315.org
1:指示客户端刷新频率。单位是秒
1;url=http://www.it315.org:指示客户端1秒后刷新到指定网址
//自动刷新 Refersh
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setHeader(“Refresh”, “2”);//2秒后刷新自己
response.setHeader( “Refresh” , “3;url=http://www.baidu.com” );
response.getWriter().write( “heihei” ); }
*Content-Disposition: attachment; filename=aaa.zip
指示客户端下载文件,并制定文件的名称
//文件下载
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取下载文件的绝对路径
String filePath = getServletContext().getRealPath( “/WEB-INF/files/2.jpg” ); //获取下载文件的绝对路径
//通知客户端已下载的方式接受数据
response.setHeader( “Content-Disposition” , “attachment;filename=2.jpg” );
//告知客户端响应正文类型
response.setHeader( “Content-Type” , “application/octet-stream” );
InputStream in = new FileInputStream(filePath);
OutputStream out = response.getOutputStream();
int len = -1;
byte b[] = new byte [1024];
while ((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close(); }
*****Set-Cookie:SS=Q0=5Lb_nQ; path=/search
服务器端发送的Cookie(会话管理)
控制浏览器不要缓存
Expires: -1
Cache-Control: no-cache (1.1)
Pragma: no-cache (1.0)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader( “Expires” , “-1” );
response.setHeader( “Cache-Control” , “no-cache” );
response.setHeader( “Pragma” , “no-cache” );
Connection: close/Keep-Alive 服务器告知是否保持连接
Date: Tue, 11 Jul 2000 18:23:51 GMT 响应时间

3、响应正文:第一个空行之后的所有内容。
浏览器要显示的数据。在未压缩的前提下,与右键 查看源码完全对应。

0 0
原创粉丝点击