码迷,mamicode.com
首页 > Web开发 > 详细

http协议

时间:2015-03-10 15:38:01      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

请求头字段:

Accept: text/html, image/*
客户机支持的数据类型
Accept-Charset: ISO-8859-1
客户机采用的编码
Accept-Encoding: gzip,compress
客户机支持的数据压缩格式
Accept-Language: en-us,zh-cn
客户机的语言环境
Host: localhost: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:
Connection: close/Keep-Alive
请求结束后,是关闭连接还是保持连接
Date: Tue, 11 Jul 2000 18:23:51 GMT
当前请求时间

响应状态码

技术分享


响应头字段:

Location: http://www.it315.org/index.jsp
配合302状态码使用,用于告诉请求找谁

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setStatus(302);
    res.setHeader(‘location‘, ‘/app/1.html‘);
}


Server: apache tomcat
web服务器名称


Content-Encoding: gzip
响应内容的数据压缩格式
Content-Length: 30
响应内容的长度

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    System.out.println("原始数据大小:" + data.getBytes().length);
    
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);
    gout.write(data.getBytes());
    gout.close();//关闭流,以保证数据传到bout中

    byte gzip[] = bout.toBtyeArray();
    System.out.println("压缩后的大小:" + data.getBytes().length);

    res.setHeader("Content-Encoding", "gzip");
    res.setHeader("Content-Length", gzip.length + "");
    res.getOutputStream().write(gzip);
}


Content-Language: zh-cn


Content-Type: text/html; charset=GB2312
响应内容的类型格式(在$tomcat/conf/web.xml文件中可以查找相关的格式对应类型)

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("Content-Type", "image/jpeg");
    InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
    int len = 0;
    byte buffer[] = new byte[1024];
    OutputStream out = res.getOutputStream();
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
}


Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT
当前资源的最后访问时间

Refresh: 3;url=http://www.baidu.com
让浏览器每3秒刷新当前页面,如果后面指定的有url则是3秒后跳转到指定url的页面

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("refresh", "3;http://www.baidu.com");
    res.getOutputStream.write("aaaa".getBytes());
}


Content-Disposition: attachment;filename=1.jpg
通知浏览器以下载方式打开数据

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("Content-Disposition", "attachment;filename=1.bmp");
    InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
    int len = 0;
    byte buffer[] = new byte[1024];
    OutputStream out = res.getOutputStream();
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
}


Transfer-Encoding: chunked
告诉浏览器数据的传送格式,这里是块传送

Set-Cookie:SS=Q0=5Lb_nQ;path=/search
与缓存相关的头信息

Etag
与缓存相关的头信息,根据web资源内容动态生成的标识字符串,用以标识资源是否发生变化,如果没有,则使用缓存,如果有则使用服务器上的资源
与If-Modified-Since不同,If-Modified-Since是秒级的,即超过1秒才更新,1秒内多次相同请求都取缓存;而Etag是实时的

Expires: -1
告诉浏览器响应数据缓存时间,-1或0不缓存

Cache-Control: no-cache
不进行缓存

Pragma: no-cache
不进行缓存

Connection: clode/Keep-Alive


Date: Tue, 11 Jul 2000 18:23:51 GMT

http协议

标签:

原文地址:http://my.oschina.net/lhplj/blog/384745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!