二、响应头信息请求头 Accept:text/html,image/* 用于告诉服务器,客户机支持的数据类型 Accept-Charset:ISO-8859-1 用于告诉服务器,客户机采用的编码 Accept-Encoding:gzip,compress 用于告诉服务器,客户机支持的数据压缩格式。 Accept-Language:en-us,zh-cn 客户机的语言环境 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 客户机通过这个头可以向服务器带一点数据。 Connection:close/Keep-Alive 客户机通过这个头告诉服务器,请求链接之后是保持连接还是关闭连接 Date:Tue,11 Jul 2000 18:23:51 GMT 时间
另:响应头 HTTP/1.1 200 OK Location: htt://www.it315.org/index.jsp 这个头配合302状态码使用,用于告诉客户找谁 Server: apache tomcat 服务器通过这个头,告诉浏览器服务器的类型 Content-Encoding: gzip 服务器通过这个头,告诉浏览器数据的压缩格式 Content-Length: 80 服务器通过这个头,告诉浏览器回送数据的长度 Content-Language: zh-cn Content-Type: text/html;charset=GB2312 服务器通过这个头,告诉浏览器回送数据的类型 Last-Modified: Tue, 11Jul 2000 18:23:51 GMT 服务器通过这个头,告诉浏览器当前浏览器的缓存时间 Refresh: l;url=http://www.it315.org 服务器通过这个头,告诉浏览器隔多长时间刷新一次 Content-Disposition: attachment; filename=aaa.zip 服务器通过这个头,告诉浏览器以下载方式打开数据 Transfer-Encoding: chunked 服务器通过这个头,告诉浏览器数据的传送格式 Set-Cookie: SS=QQ=5Lb_nQ; path=/search ETag: W/*7777-1223323232 缓存相关的头 Expires: -1 服务器通过这个头,告诉浏览器把回送的数据缓存多长时间,-1或0,则不缓存。 Cache-Control: no-cache Pragma: no-cache 服务器通过以上两个头,也是控制浏览器不要缓存数据。 Connection: close/Keep-Alive Date: Thu, 13 Jul 2000 05:46:53 GMT
![http请求响应参数解读 - freedomheaven - freedomheaven的博客](http://img0.ph.126.net/irizgH3j80oOqEHcpOajDg==/6608230909190154063.png)
public class ServletDemo1 extends HttpService { public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("content-disposition", "attachment;filename=3.jpg"); InputStream in = this.getServletContext().getResourceAsStream("/3.bmp"); int len = 0; byte[] buffer = new byte[1024]; OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0) { out.write(buffer, 0, len); } } //定义文件下载 public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("content-disposition", "attachment;filename=3.jpg"); InputStream in = this.getServletContext().getResourceAsStream("/3.bmp"); int len = 0; byte[] buffer = new byte[1024]; OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0) { out.write(buffer, 0, len); } } //定义刷新 public void test4(HttpServletRequest request, HttpServletResponse response) throws Exception { //response.setHeader("refresh", "3"); response.setHeader("refresh", "3;url='http://www.baidu.com'"); String data = "aaaa"; response.getOutputStream().write(data.getBytes()); } //通过content-type头字段,控制浏览器以哪种方式处理数据 public void test3(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("content-type", "image/bmp"); InputStream in = this.getServletContext().getResourceAsStream("/1.bmp"); int len = 0; byte[] buffer = new byte[1024]; OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0) { out.write(buffer, 0, len); } } //压缩数据输出 public void test2(HttpServletRequest request, HttpServletResponse response) throws Exception { String data = "aaaaa"; ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(data.getBytes()); gout.close(); byte gzip[] = bout.toByteArray(); //得到压缩后的数据 //通知浏览器数据采用的压缩格式 response.setHeader("Content-Encoding", "gzip"); response.setHeader("Content-Lenght", gzip.length+""); response.getOutputStream().write(gzip); } //用location和302实现请求重定向 public void test1(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setStatus(302); response.setHeader("location", "/day04/1.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { } }
![http请求响应参数解读 - freedomheaven - freedomheaven的博客](http://img2.ph.126.net/-W3asmBh1IZmsxJnin-t-A==/3024730099833044324.png)
package com.fly.ie; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * 利用range属性实现断点续传 * @author flyings * */ public class RangeDemo { public static void main(String[] args) throws Exception { //定义链接 URL url = new URL("http://localhost:8080/day04/a.txt"); //打开链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //定义请求头range属性 conn.setRequestProperty("Range", "bytes=5-"); InputStream in = conn.getInputStream(); int len = 0; byte[] buff = new byte[1024]; //续写到本地文件 FileOutputStream out = new FileOutputStream("c://a.txt", true); while((len=in.read(buff))>0) { out.write(buff, 0, len); } in.close(); out.close(); } }