欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Http请求头和响应头

程序员文章站 2022-06-17 16:02:27
...

原文链接:http://blog.csdn.net/eson_15/article/details/51259312

这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例。

1. HTTP请求头

accept:浏览器通过这个头告诉服务器,它所支持的数据类型。如:text/html, image/jpeg
accept-Charset:浏览器通过这个头告诉服务器,它支持哪种字符集。
accept-encoding:浏览器通过这个头告诉服务器,它支持哪种压缩格式。
accept-language:浏览器通过这个头告诉服务器,它的语言环境。
host:浏览器通过这个头告诉服务器,它想访问哪台主机。
if-modified-since:浏览器通过这个头告诉服务器,缓存数据的时间
referer:浏览器通过这个头告诉服务器,客户机是哪个页面来的(防盗链)。
Connection:浏览器通过这个头告诉服务器,请求完后是断开链接还是维持链接。

2. HTTP响应头

location:服务器通过这个头告诉浏览器跳到哪里。
server:服务器通过这个头告诉浏览器服务器的型号。
content-encoding:服务器通过这个头告诉浏览器数据的压缩格式。
content-length:服务器通过这个头告诉浏览器回送数据的长度。
content-language:服务器通过这个头告诉浏览器语言环境。
content-type:服务器通过这个头告诉浏览器回送数据的类型。
refresh:服务器通过这个头告诉浏览器定时刷新。
content-disposition:服务器通过这个头告诉浏览器以下载方式打开数据。
transfer-encoding:服务器通过这个头告诉浏览器数据是以分块方式回送的
以下三个表示服务器通过这个头告诉浏览器不要缓存
expires:-1
cache-control:no-cache
pragma:no-cache

3. 响应头在开发中的示例

  1. public class ServletDemo extends HttpServlet {  
  2.   
  3.     @Override  
  4.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  5.             throws ServletException, IOException {  
  6.           
  7. //      locationTest(resp);       
  8. //      zipTest(resp);  
  9.         contentTypeTest(resp);   
  10. //      refreshTest(resp);  
  11.           
  12.     }  
  13.   
  14.     //指定浏览器定时刷新  
  15.     private void refreshTest(HttpServletResponse resp) throws IOException {  
  16. //      resp.setHeader(“refresh”, “3”);//3秒钟刷新一次  
  17.         resp.setHeader(”refresh”“3;url=’http://www.baidu.com’”);//隔三秒跳转到百度  
  18.         resp.getWriter().write(”java web”);  
  19.     }  
  20.       
  21.     //指定回送数据类型以及以下载方式呈现到浏览器  
  22.     private void contentTypeTest(HttpServletResponse resp) throws IOException {  
  23.           
  24.         resp.setHeader(”Content-type”“image/jpeg”);//关于类型的value值,可以在tomcat目录下的\config\web.xml中找到  
  25.         resp.setHeader(”Content-Disposition”“attachment;filename=dog.jpg”);//告诉浏览器以下载的方式呈现给用户  
  26.         InputStream in = this.getServletContext().getResourceAsStream(“/dog.jpg”);  
  27.           
  28.         //读写模板代码  
  29.         byte buffer[] = new byte[1024];  
  30.         int len = 0;  
  31.         OutputStream out = resp.getOutputStream();//拿到响应的输出流  
  32.         while((len = in.read(buffer)) > 0) {  
  33.             out.write(buffer, 0, len);  
  34.         }  
  35.     }  
  36.   
  37.     //数据压缩  
  38.     private void zipTest(HttpServletResponse resp) throws IOException {  
  39.         String data = ”java web学习 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”;  
  40.         System.out.println(”原始数据的大小:” + data.getBytes().length);  
  41.           
  42.         //获得一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据的不断写入而自动增长  
  43.         //可使用toByteArray()和toString()获得数据  
  44.         ByteArrayOutputStream bout = new ByteArrayOutputStream();     
  45.         GZIPOutputStream gout = new GZIPOutputStream(bout);//获得一个压缩流,参数为一个输出流,这个输出流捕获压缩后的数据并将其写到浏览器  
  46.         gout.write(data.getBytes());//调用压缩流的write方法即可将指定数据进行压缩,参数为字节流数据  
  47.         gout.close();//将缓冲数据写到流中  
  48.           
  49.         //获得压缩后的数据  
  50.         byte g[] = bout.toByteArray();  
  51.         System.out.println(”压缩数据的大小:” + g.length);  
  52.           
  53.         resp.setHeader(”Content-Encoding”“gzip”);//通知浏览器这是压缩数据  
  54.         resp.setHeader(”Content-Length”, g.length + “”);//并告诉浏览器压缩数据的长度  
  55.         resp.getOutputStream().write(g);//打印到浏览器中  
  56.     }  
  57.   
  58.     //location头的应用  
  59.     private void locationTest(HttpServletResponse resp) {  
  60.         resp.setStatus(302);//  
  61.         resp.setHeader(”location”“/day04/MyHtml.html”);//服务器通过location告诉浏览器跳转到MyHtml.html页面执行   
  62.     }  
  63.   
  64.     @Override  
  65.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  66.             throws ServletException, IOException {  
  67.   
  68.         doGet(req, resp);         
  69.     }  
  70. }  
public class ServletDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

//      locationTest(resp);     
//      zipTest(resp);
        contentTypeTest(resp); 
//      refreshTest(resp);

    }

    //指定浏览器定时刷新
    private void refreshTest(HttpServletResponse resp) throws IOException {
//      resp.setHeader("refresh", "3");//3秒钟刷新一次
        resp.setHeader("refresh", "3;url='http://www.baidu.com'");//隔三秒跳转到百度
        resp.getWriter().write("java web");
    }

    //指定回送数据类型以及以下载方式呈现到浏览器
    private void contentTypeTest(HttpServletResponse resp) throws IOException {

        resp.setHeader("Content-type", "image/jpeg");//关于类型的value值,可以在tomcat目录下的\config\web.xml中找到
        resp.setHeader("Content-Disposition", "attachment;filename=dog.jpg");//告诉浏览器以下载的方式呈现给用户
        InputStream in = this.getServletContext().getResourceAsStream("/dog.jpg");

        //读写模板代码
        byte buffer[] = new byte[1024];
        int len = 0;
        OutputStream out = resp.getOutputStream();//拿到响应的输出流
        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    }

    //数据压缩
    private void zipTest(HttpServletResponse resp) throws IOException {
        String data = "java web学习 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        System.out.println("原始数据的大小:" + data.getBytes().length);

        //获得一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据的不断写入而自动增长
        //可使用toByteArray()和toString()获得数据
        ByteArrayOutputStream bout = new ByteArrayOutputStream();   
        GZIPOutputStream gout = new GZIPOutputStream(bout);//获得一个压缩流,参数为一个输出流,这个输出流捕获压缩后的数据并将其写到浏览器
        gout.write(data.getBytes());//调用压缩流的write方法即可将指定数据进行压缩,参数为字节流数据
        gout.close();//将缓冲数据写到流中

        //获得压缩后的数据
        byte g[] = bout.toByteArray();
        System.out.println("压缩数据的大小:" + g.length);

        resp.setHeader("Content-Encoding", "gzip");//通知浏览器这是压缩数据
        resp.setHeader("Content-Length", g.length + "");//并告诉浏览器压缩数据的长度
        resp.getOutputStream().write(g);//打印到浏览器中
    }

    //location头的应用
    private void locationTest(HttpServletResponse resp) {
        resp.setStatus(302);//
        resp.setHeader("location", "/day04/MyHtml.html");//服务器通过location告诉浏览器跳转到MyHtml.html页面执行 
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        doGet(req, resp);       
    }
}

4. HTTP响应状态码

100-199:表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程
200-299:表示成功接收请求并已完成整个处理过程,常用200
300-399:未完成请求,客户需进一步细化请求,常用302,307,304
400-499:客户端的请求有错误,常用404
500-599:服务器端出现错误,常用500

_____________________________________________________________________________________________________________________________________________________

—–乐于分享,共同进步!

—–我的博客主页:http://blog.csdn.net/eson_15

原文链接:http://blog.csdn.net/eson_15/article/details/51259312

这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例。

1. HTTP请求头

accept:浏览器通过这个头告诉服务器,它所支持的数据类型。如:text/html, image/jpeg
accept-Charset:浏览器通过这个头告诉服务器,它支持哪种字符集。
accept-encoding:浏览器通过这个头告诉服务器,它支持哪种压缩格式。
accept-language:浏览器通过这个头告诉服务器,它的语言环境。
host:浏览器通过这个头告诉服务器,它想访问哪台主机。
if-modified-since:浏览器通过这个头告诉服务器,缓存数据的时间
referer:浏览器通过这个头告诉服务器,客户机是哪个页面来的(防盗链)。
Connection:浏览器通过这个头告诉服务器,请求完后是断开链接还是维持链接。

2. HTTP响应头

location:服务器通过这个头告诉浏览器跳到哪里。
server:服务器通过这个头告诉浏览器服务器的型号。
content-encoding:服务器通过这个头告诉浏览器数据的压缩格式。
content-length:服务器通过这个头告诉浏览器回送数据的长度。
content-language:服务器通过这个头告诉浏览器语言环境。
content-type:服务器通过这个头告诉浏览器回送数据的类型。
refresh:服务器通过这个头告诉浏览器定时刷新。
content-disposition:服务器通过这个头告诉浏览器以下载方式打开数据。
transfer-encoding:服务器通过这个头告诉浏览器数据是以分块方式回送的
以下三个表示服务器通过这个头告诉浏览器不要缓存
expires:-1
cache-control:no-cache
pragma:no-cache

3. 响应头在开发中的示例

  1. public class ServletDemo extends HttpServlet {  
  2.   
  3.     @Override  
  4.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  5.             throws ServletException, IOException {  
  6.           
  7. //      locationTest(resp);       
  8. //      zipTest(resp);  
  9.         contentTypeTest(resp);   
  10. //      refreshTest(resp);  
  11.           
  12.     }  
  13.   
  14.     //指定浏览器定时刷新  
  15.     private void refreshTest(HttpServletResponse resp) throws IOException {  
  16. //      resp.setHeader(“refresh”, “3”);//3秒钟刷新一次  
  17.         resp.setHeader(”refresh”“3;url=’http://www.baidu.com’”);//隔三秒跳转到百度  
  18.         resp.getWriter().write(”java web”);  
  19.     }  
  20.       
  21.     //指定回送数据类型以及以下载方式呈现到浏览器  
  22.     private void contentTypeTest(HttpServletResponse resp) throws IOException {  
  23.           
  24.         resp.setHeader(”Content-type”“image/jpeg”);//关于类型的value值,可以在tomcat目录下的\config\web.xml中找到  
  25.         resp.setHeader(”Content-Disposition”“attachment;filename=dog.jpg”);//告诉浏览器以下载的方式呈现给用户  
  26.         InputStream in = this.getServletContext().getResourceAsStream(“/dog.jpg”);  
  27.           
  28.         //读写模板代码  
  29.         byte buffer[] = new byte[1024];  
  30.         int len = 0;  
  31.         OutputStream out = resp.getOutputStream();//拿到响应的输出流  
  32.         while((len = in.read(buffer)) > 0) {  
  33.             out.write(buffer, 0, len);  
  34.         }  
  35.     }  
  36.   
  37.     //数据压缩  
  38.     private void zipTest(HttpServletResponse resp) throws IOException {  
  39.         String data = ”java web学习 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”;  
  40.         System.out.println(”原始数据的大小:” + data.getBytes().length);  
  41.           
  42.         //获得一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据的不断写入而自动增长  
  43.         //可使用toByteArray()和toString()获得数据  
  44.         ByteArrayOutputStream bout = new ByteArrayOutputStream();     
  45.         GZIPOutputStream gout = new GZIPOutputStream(bout);//获得一个压缩流,参数为一个输出流,这个输出流捕获压缩后的数据并将其写到浏览器  
  46.         gout.write(data.getBytes());//调用压缩流的write方法即可将指定数据进行压缩,参数为字节流数据  
  47.         gout.close();//将缓冲数据写到流中  
  48.           
  49.         //获得压缩后的数据  
  50.         byte g[] = bout.toByteArray();  
  51.         System.out.println(”压缩数据的大小:” + g.length);  
  52.           
  53.         resp.setHeader(”Content-Encoding”“gzip”);//通知浏览器这是压缩数据  
  54.         resp.setHeader(”Content-Length”, g.length + “”);//并告诉浏览器压缩数据的长度  
  55.         resp.getOutputStream().write(g);//打印到浏览器中  
  56.     }  
  57.   
  58.     //location头的应用  
  59.     private void locationTest(HttpServletResponse resp) {  
  60.         resp.setStatus(302);//  
  61.         resp.setHeader(”location”“/day04/MyHtml.html”);//服务器通过location告诉浏览器跳转到MyHtml.html页面执行   
  62.     }  
  63.   
  64.     @Override  
  65.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  66.             throws ServletException, IOException {  
  67.   
  68.         doGet(req, resp);         
  69.     }  
  70. }  
public class ServletDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

//      locationTest(resp);     
//      zipTest(resp);
        contentTypeTest(resp); 
//      refreshTest(resp);

    }

    //指定浏览器定时刷新
    private void refreshTest(HttpServletResponse resp) throws IOException {
//      resp.setHeader("refresh", "3");//3秒钟刷新一次
        resp.setHeader("refresh", "3;url='http://www.baidu.com'");//隔三秒跳转到百度
        resp.getWriter().write("java web");
    }

    //指定回送数据类型以及以下载方式呈现到浏览器
    private void contentTypeTest(HttpServletResponse resp) throws IOException {

        resp.setHeader("Content-type", "image/jpeg");//关于类型的value值,可以在tomcat目录下的\config\web.xml中找到
        resp.setHeader("Content-Disposition", "attachment;filename=dog.jpg");//告诉浏览器以下载的方式呈现给用户
        InputStream in = this.getServletContext().getResourceAsStream("/dog.jpg");

        //读写模板代码
        byte buffer[] = new byte[1024];
        int len = 0;
        OutputStream out = resp.getOutputStream();//拿到响应的输出流
        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    }

    //数据压缩
    private void zipTest(HttpServletResponse resp) throws IOException {
        String data = "java web学习 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        System.out.println("原始数据的大小:" + data.getBytes().length);

        //获得一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据的不断写入而自动增长
        //可使用toByteArray()和toString()获得数据
        ByteArrayOutputStream bout = new ByteArrayOutputStream();   
        GZIPOutputStream gout = new GZIPOutputStream(bout);//获得一个压缩流,参数为一个输出流,这个输出流捕获压缩后的数据并将其写到浏览器
        gout.write(data.getBytes());//调用压缩流的write方法即可将指定数据进行压缩,参数为字节流数据
        gout.close();//将缓冲数据写到流中

        //获得压缩后的数据
        byte g[] = bout.toByteArray();
        System.out.println("压缩数据的大小:" + g.length);

        resp.setHeader("Content-Encoding", "gzip");//通知浏览器这是压缩数据
        resp.setHeader("Content-Length", g.length + "");//并告诉浏览器压缩数据的长度
        resp.getOutputStream().write(g);//打印到浏览器中
    }

    //location头的应用
    private void locationTest(HttpServletResponse resp) {
        resp.setStatus(302);//
        resp.setHeader("location", "/day04/MyHtml.html");//服务器通过location告诉浏览器跳转到MyHtml.html页面执行 
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        doGet(req, resp);       
    }
}

4. HTTP响应状态码

100-199:表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程
200-299:表示成功接收请求并已完成整个处理过程,常用200
300-399:未完成请求,客户需进一步细化请求,常用302,307,304
400-499:客户端的请求有错误,常用404
500-599:服务器端出现错误,常用500

_____________________________________________________________________________________________________________________________________________________

—–乐于分享,共同进步!

—–我的博客主页:http://blog.csdn.net/eson_15
相关标签: Http请求响应