手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Web资源访问及HTTP协议详解

[日期:2012-07-22] 来源:Linux社区  作者:xiazdong [字体:大 中 小]

2.HTTP响应 

 

2.1 响应行 

2.2.1 状态码 

100-199 服务器成功接收客户请求,不过需要让客户机继续发送请求完成整个请求过程
200-299 200 成功处理
300-399 302 : 找别人,在响应头中需要有location属性;    304、307:找缓存,即缓存中存在页面
400-499 403:存在页面,但是用户没有权限访问;    404:无此页面
500-599 500 : 服务器内部错误

2.2 响应头

Server 服务器信息
Content-Length 响应内容的长度
Content-Encoding 服务器内容的压缩格式
Content-Type 服务器内容的格式,比如text/html ; image/jpeg
location

用于状态码为302,表示别人的地址

Content-disposition 下载返回的数据
Transfer-Encoding 以块传输还是以字节传输
Date 时间
refresh 定时刷新或重定向
Last-Modified 最后改变的时间
Expires -1或0 表示无缓存
ETag 每个web资源都有一个标示符,只要web资源内容改动,标示符就会改变;

应用1:location重定向

  1. response.setStatus(302);  
  2.   
  3. response.setHeader("location","http://localhost:8080/a/1.html");  

应用2:content-Encoding  压缩数据

  1. response.setHeader("content-encoding","gzip");  
  2.   
  3. ByteArrayOutputStream bout = new ByteArrayOutputStream();  
  4.   
  5. GZIPOutputStream out = new GZIPOutputStream(bout);  
  6.   
  7. out.write("Hello".getBytes());  
  8.   
  9. out.close();  
  10.   
  11. byte[] b = bout.toByteArray();  
  12.   
  13. response.getOutputStream().write(b);  
     

电信对于网站的收费是按照出口流量收费的,因此需要压缩数据,减少费用;

应用3:content-disposition  

response.setHeader("content-disposition","attachment;filename=1.jpg");

应用4:refresh

response.setHeader("refresh","3");  定时更新 用于实时软件

response.setHeader("refresh","3;url='http://localhost:8080/1.html'");  注册登录

应用5:content-type   返回的数据类型;

  1. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.         response.setHeader("content-type", "image/ipeg");  
  3.         InputStream in = this.getServletContext().getResourceAsStream("/1.jpg");  
  4.         Java.io.OutputStream out = response.getOutputStream();  
  5.         byte[]b = new byte[1024];  
  6.         int length = 0;  
  7.         while((length=in.read(b))>0){  
  8.             out.write(b, 0, length);  
  9.         }  
  10.         out.close();  
  11.       
  12.     }  
linux
相关资讯       HTTP协议 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款