黑马程序员_http请求响应参数解读

------- android培训 java培训 、期待与您交流! ---------- 
一、请求头信息

请求头 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 服务器通过这个头,告诉浏览器把回送的数据缓存多长时间,-10,则不缓存。 Cache-Control: no-cache Pragma: no-cache 服务器通过以上两个头,也是控制浏览器不要缓存数据。 Connection: close/Keep-Alive Date: Thu, 13 Jul 2000 05:46:53 GMT

另:
1、http响应的状态码如下:
http请求响应参数解读 - freedomheaven - freedomheaven的博客

 2、响应讲解代码如下:

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 { } }


三、range之断点续传
1、range参数如下:
http请求响应参数解读 - freedomheaven - freedomheaven的博客
 
2、代码

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(); } }


----------------------- android培训java培训、java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值