现在的位置: 首页 > 综合 > 正文

HTTP1.1协议常用请求头和响应头内容–精简版

2017年11月20日 ⁄ 综合 ⁄ 共 4842字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

请求头

Accept:text/html,image 用于告诉服务器, 客户机支持的数据类型

Accept-Charaset:ISO-8859-1 客户机支持的编码

Accept-Encoding:gzip 客户机支持的数据压缩格式

Accept-Language: zh-cn 客户机支持的语言

Host:www.t381.org 告诉服务器,想访问的主机名

Referer:http://www.ti23.org 从哪个资源访问服务器(常用于防盗链)

User-Agent:Mozilla 4.0 告诉客户机的软件环境

Cookie:可以带给服务器客户端的数据

Connection:close/Keep-Alive 请求后是关闭,还是保持连接

Date: Tue, 11 Jul 2000 18:30:30 GMT 请求时间

响应头
HTTP/1.1 200 OK 状态行,200是状态码表示一切OK

Location:
http://www.aaa.net
配合http302状态码实现重定向,告诉客户机应该找谁

Server: Microsoft-IIS/5.0  告诉浏览器服务器的类型

Content-Encoding:gzip 数据的压缩格式

Content-Length:告诉浏览器,回送数据的长度

Content-Type: text/html; charset=utf-8 告诉浏览器,回送数据的类型

Transfer-Encoding:告诉浏览器数据的传送格式

Refresh:多长时间之后刷新一次

Last-Modified: 告诉浏览器当前资源的缓存时间点

Content-Disposition:"attachment;filename=mother.jpg" 控制让浏览器用下载的方式访问资源

Connection:close/Keep-Alive

Date: Sun, 23 Sep 2012 13:03:14 GMT

Expires:告诉浏览器把回送的资源缓存多长时间。-1或0,则表示不缓存,单位为毫秒,如缓存1小时:

        response.setDateHeader("Expires", System.currentTimeMillis()+1000*3600);

Cache-Contorl: no-cache或max-age=1800 控制浏览器的缓存

Pragma: no-cache

注意:后三个都可以实现是否缓存以及缓存多久,但是不同浏览器对其支持不同,所以如果想要不缓存,则可以三者都设置一下,以支持所有浏览器


location 响应头的用法:

//用location响应头和状态码302实现请求重定向

private void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   //和下句代码可互换位置,此两行相当于response.sendRedirect("");
   response.setStatus(302);

   //或者"location.jsp"
   response.setHeader("location","/HTTPHeader/location.jsp");

}    

则在浏览器上输入:http://localhost:8080/HTTPHeader/header,会跳转至http://localhost:8080/HTTPHeader/location.jsp页面。

用HttpWatch检测得到的响应头信息:

   HTTP/1.1 302 Moved Temporarily
   Server: Apache-Coyote/1.1
   location: location.jsp
   Content-Length: 0
   Date: Fri, 23 Dec 2011 13:36:23 GMT


Content-Encoding 响应头的用法:

 //压缩数据输出:

private void contentEncoding(HttpServletRequest request,

HttpServletResponse response) throws IOException {

       String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
         System.out.println("原始数据大小:"+data.getBytes().length);
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         
          //包装输出流
         GZIPOutputStream gout = new GZIPOutputStream(bout);
         gout.write(data.getBytes());

         /*
          * 调用该方法或gout.flush()后,GZIPOutputStream才会将经过压缩的data.getBytes()字节
          * 数组写入ByteArrayOutputStream
          */

         gout.close();

         byte gzip[] = bout.toByteArray();
         System.out.println("压缩后的大小:"+gzip.length);
         
         //通知浏览器数据采用压缩格式
         response.setHeader("Content-Encoding", "gzip");

         //通过浏览器会送数据的长度
         response.setHeader("Content-Length", gzip.length+"");

         response.getOutputStream().write(gzip);  

}

后台打印:

    原始数据大小:666
    压缩后的大小:27

用HttpWatch检测得到的响应头信息:

   HTTP/1.1 200 OK
   Server: Apache-Coyote/1.1
   Content-Encoding: gzip
   Content-Length: 27
   Date: Fri, 23 Dec 2011 13:59:30 GMT

   ?????????


Content-type 响应头的用法:

//通过Content-Type控制以哪种方式处理数据

 private void contentType(HttpServletRequest request,HttpServletResponse response) throws IOException {

     /**
      * 关于Content-Type响应头的取值:可以参考...\Tomcat 6.0\conf\web.xml
         <mime-mapping>
                <extension>jpg</extension>
                <mime-type>image/jpeg</mime-type>
             </mime-mapping>
      **/
     response.setHeader("Content-type", "image/jpeg");
     InputStream in = this.getServletContext().getResourceAsStream("/mother.jpg");
     int len = 0;
     byte buffer[] = new byte[1024];
     OutputStream out = response.getOutputStream();
     while ((len=in.read(buffer))>0) {
        out.write(buffer,0,len);
     }

用HttpWatch检测得到的响应头信息:

   HTTP/1.1 200 OK
   Server: Apache-Coyote/1.1
   Content-Type: image/jpeg
   Transfer-Encoding: chunked  
   Date: Fri, 23 Dec 2011 14:18:10 GMT


Refresh响应头的用法:

//定时刷新

private void refresh(HttpServletRequest request,

HttpServletResponse response) throws IOException {

     //每隔3秒刷新一次当前页面
    // response.setHeader("Refresh", "3");
   
    //3秒后跳转至本工程下的mother.jpg下,地址栏会变,重定向技术
    response.setHeader("Refresh", "3;url='/HTTPHeader/mother.jpg'");

    String data = "aaaaaaaaaaaaaaaaaaaaaa";
    response.getOutputStream().write(data.getBytes());
}

Content-Disposition响应头的用法:

//控制让浏览器用下载的方式访问资源

private void contentDisposition(HttpServletRequest request,

HttpServletResponse response) throws IOException {

//控制让浏览器用下载的方式访问资源
     response.setHeader("Content-Disposition", "attachment;filename=mother.jpg");
     InputStream in = this.getServletContext().getResourceAsStream("mother.jpg");

     int len = 0;
     byte[] buffer = new byte[1024];
     OutputStream out = response.getOutputStream();

     while ((len=in.read(buffer))>0) {
        out.write(buffer,0,len);
     }
}

用HttpWatch检测得到的响应头信息:

   HTTP/1.1 200 OK
   Server: Apache-Coyote/1.1
   Content-Disposition: attachment;filename=mother.jpg
   Transfer-Encoding: chunked
   Date: Fri, 23 Dec 2011 14:41:18 GMT

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

抱歉!评论已关闭.