黑马程序员 10 HTTP协议学习笔记

来源:互联网 发布:深圳加工中心编程招聘 编辑:程序博客网 时间:2024/06/06 19:40
                                              ------- android培训、java培训、期待与您交流! --------

Http协议

Created Sunday 14 January 2013 with Gvim

超本文传输协议 (HTTP, hyperText Transfer Protoco)

  1. http协议是建立再TCP/IP协议上的
  2. http协议1.0,1.1版本,目前通用的是1.1版本
http 1.0 称为短连接 <持续时间的长短>
http 1.1 称为长连接
  1. http的请求部分 <基本结构> <并不是每一次请求的消息头都是一样的>
          GET / HTTP/1.1 请求行
Referer :http://URL <防盗链> 多个消息头 [ 消息名 : 内容 ]
空行
内容
  1. 详解http请求消息头
    • Accept : text/html,image/* [告诉浏览器 可以接受的类型]
    • Accept- Charset : ISO-8859-1 [接受字符编码集]
    • Accept - Encoding gzip.compress [可以接受 gzip,compress压缩后的数据]
    • Accept - Language : en_us,zh_cn [浏览器支持中英文]
    • Host : www.sohu.com:80 [ 我要找的主机名 ]
    • If-Modified-Since: Tue, 11 jul 2000 18:30:30 GMT [ 告诉服务器,浏览器缓存中有这个资源文件 该文件的时间是....]
    • Referer : http://www.sohu,com/index.jsp [告诉服务器 我来自哪里 该消息头常用于防止盗链]
    • User - Agent : Mozilla/4.0 (compatible ; MSIE 5,5) [ 告诉服务器浏览器内核 ]
    • Cookie: [ cookie ]
    • Connection: close/keep-Alive [保持连接,发完数据后,不关闭连接]
    • Date [当前时间]
  2. Request.getHeader(); 获取头信息
实现防盗链 Referer
String referer = Request.getHeader("Referer");
if(referer==null || !referer.startwith("http://myurl")){}
  1. http响应
一个http响应就代表了服务器向客户端发送的数据
基本结构
HTTP/1.1 200 OK [状态行]
HTTP版本号 状态码 原因叙述
状态码分位5种
100~199 表示成功接受请求 要求客户端继续提交下一个请求
200~299 表示成功接受请求并完成处理
300~399
400~499
500~599
常见状态:
200 就是整个请求和响应过程没有发生错误
302 表示当请求某个资源的时候 服务器让浏览器转向到另外一个资源 比如:response.serRedireset("URL");
404 找不到资源
500 服务器端错误 Internal Server Error
  1. http响应消息头详解
Location :http://URL [让浏览器重新定位]
Server: apache tomcat [告诉浏览器WEB服务器的名称]
Content-encodeing: gzip [告诉浏览器使用压缩方法]
Content-Length : 80 [告诉浏览器发送的数据80字节]
Content-language: zh_cn [支持中文]
Content-Type : text/html; [内容格式]
Last-Modeified: Tue, 11 jul 2000 18:30:30 GMT[ 告诉浏览器该资源最近更新的时间]
Refresh: 1;url=http:www.baidu.com [过多久刷新到URL] (定时刷新)
Content-Disposition : attachment;filename=aaa.zip [ 告诉浏览器有文件需要下载 ]

  1. Content-Disposition WEB下载案例
response.setContentType("text/html"); //设置发送方式
response.setHeader("Content-Disposition","attachment;filename=code357.jpg");
//获取要下载文件的全路径
String path = this.getServletContext().getRealPath("/images/code357.jpg");
FileInputStream fis = new FileInputStream(path); //创建文件输入流
byte buff[]=new byte[1024]; //做一个缓冲字节数组
int len = 0; //表示每次实际读取到的字节
OutputStream os = response.getOutputStream();
int len=0;
while((len=fis.read(buff))>0){os.write(buff,0,len);}
	
				
		
原创粉丝点击