请求头字段:
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 当前请求时间Range: 指示服务器只传输一部分web资源.可以用来实现断点续传功能 Range字段可以通过3种格式设置要传输的字节范围:Range: bytes=1000-2000 传输范围从1000到2000字节Range: bytes=1000- 传输第1000个字节以后的所有内容Range: bytes=10000 传输最后1000个字节public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/day05/a.txt"); HttpURLConnection = conn = (HttpURLConneection)url.openConnection(); conn.setRequestProperty("Range", "bytes=5-"); InputStream in = conn.getInputStream(); //第2个参数true 以追加方式写入文件,而不是覆盖 FileOutputStream out = new FileOutputStream("C:\\a.txt", true); int len = 0; byte[] buffer = new byte[1024]; while((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); in.close();}
响应状态码
响应头字段:
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
是文本的上一次修改时间,并不是“当前资源的最后访问时间”。客户端收到服务器的带有Last-modified的文本之后会保存在本地,当客户端访问上次的内容时。客户端的请求头部会带上:If-Modified-Since字段,服务器收到之后会检查这个时间,查看文本在这个时间之后是否修改过,如果没有,则返回304----没有更新(不反悔内容)给客户,客户收到304就调用缓存给客户显示内容;否则返回一个新的文本,Last-modified变为最新的修改时间给客户。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: close/Keep-Alive
Date: Tue, 11 Jul 2000 18:23:51 GMTAccept-Ranges: bytes说明web服务器支持Range
Accept-Ranges: none说明web服务器不支持Range
Content-Ranges: 1000-3000/5000指定了返回的web资源的字节范围为1000-3000,总量是5000