HTTP协议请求头信息和响应头信息

http的请求部分

基本结构

  1. 请求行 GET  /test/hello.html HTTP/1.1
  2. 消息头(并不是每一次请求都一样)
  3. 空行
  4. 内容(内容名字=内容体)

常用请头信息

  • Accept:text/html,image/*(告诉服务器,浏览器可以接受文本,网页图片)
  • Accept-Charaset:ISO-8859-1 [接受字符编码:iso-8859-1]
  • Accept-Encoding:gzip,compress[可以接受  gzip,compress压缩后数据]
  • Accept-Language:zh-cn[浏览器支持的语言]
  • Host:localhost:8080[浏览器要找的主机]
  • IF-MODIFIED-Since:Tue,11Jul 2000 18:23:51[告诉服务器我这缓存中有这个文件,该文件的时间]
  • Referer:http://localhost:8080/test/abc.html[告诉服务器我来自哪里,常用于防止下载,盗链]
  • User-Agent:Nozilla/4.0(Com...)[告诉服务器我的浏览器内核]
  • Cookie:[Cookie,常用于认证]
  • Connection:close/Keep-Alive [保持链接,发完数据后,我不关闭链接]
  • Date:[浏览器发送数据的请求时间]

常用响应头信息

  1. location:http://www.baidu.org/index.jsp
  2. server:apache tomcat [告诉浏览器我是tomcat]
  3. Content-Encoding:gzip[告诉浏览器我使用了gzip]
  4. Content-Lenght:80 [告诉浏览器回送的数据大小]
  5. Content-Language:zh-cn[支持中文]
  6. Content-Type:text/html;charset=gb2312[内容格式和编码]
  7. Last-Modified:Tue,11 Juj,2000 18 18:29:20[告诉浏览器该资源上次更新
  8.        时间是多少]
  9. Refresh:1;url=http://www.baidu.com[过多久刷新到哪里去]
  10. Content-Disposition;attachment;filename=aaa.zip[告诉浏览器有文件下载]
  11. Transfer-Encoding:chunked[传输编码]
  12. Set-Cookie:
  13. Expires:-1[告诉浏览器如何缓存页面]
  14. cache-Control:[告诉浏览器如何缓存页面(因为浏览器的兼容性最好设置两个)]
  15. pragma:no-cache
  16. Connection:close/Keep-Alive
  17. Date:Tue,11 Jul 2000 18:23:51

(1)有些网站对及时性比较高,我们不缓存页面
response.setDateHeader("Expires",-1);
//为了保证兼容性
response.setHeader("Cache-Control","no-cache")
response.setHeader("Pragma","no-cache")
(2)有些网站要求网页缓存一定时间,比如缓存一个小时
response.setDateHeader("Expires",System.currentimeMillis()*3600*1000*24);

通用信息头
Cache-Control:no-cache
Pragma:no-cache
Connection:close/Keep-Alive

Date:时间

上一篇:eclipse及Java常用问题及解决办法汇总


下一篇:Python多进程multiprocessing使用示例