http请求头,响应头字段

请求头字段:

Accept: text/html, image/*
客户机支持的数据类型
Accept-Charset: ISO-8859-1
客户机采用的编码
Accept-Encoding: gzip,compress
客户机支持的数据压缩格式
Accept-Language: en-us,zh-cn
客户机的语言环境
Host: localhost:80
访问的主机名
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT
资源的缓存时间,用以判断服务器上的资源是否发生变化
Referer: http://www.it315.org/index.jsp
从哪个页面跳转来访问资源的,用以实现防盗链
User-Agent: Mozilla/4.0 (compatible;MSIE 5.5;Windows NT 5.0)
客户机的软件环境
Cookie:
Connection: close/Keep-Alive
请求结束后,是关闭连接还是保持连接
Date: Tue, 11 Jul 2000 18:23:51 GMT
当前请求时间
Range:
指示服务器只传输一部分web资源.可以用来实现断点续传功能
Range字段可以通过3种格式设置要传输的字节范围:
Range: bytes=1000-2000
传输范围从1000到2000字节
Range: bytes=1000-
传输第1000个字节以后的所有内容
Range: bytes=10000
传输最后1000个字节

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:8080/day05/a.txt");
    HttpURLConnection = conn = (HttpURLConneection)url.openConnection();
    conn.setRequestProperty("Range", "bytes=5-");

    InputStream in = conn.getInputStream();
    //第2个参数true 以追加方式写入文件,而不是覆盖
    FileOutputStream out = new FileOutputStream("C:\\a.txt", true);
    int len = 0;
    byte[] buffer = new byte[1024];
    while((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    out.close();
    in.close();
}



响应状态码

http请求头,响应头字段_第1张图片


响应头字段:

Location: http://www.it315.org/index.jsp
配合302状态码使用,用于告诉请求找谁

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setStatus(302);
    res.setHeader('location', '/app/1.html');
}


Server: apache tomcat
web服务器名称


Content-Encoding: gzip
响应内容的数据压缩格式
Content-Length: 30
响应内容的长度

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    System.out.println("原始数据大小:" + data.getBytes().length);
    
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);
    gout.write(data.getBytes());
    gout.close();//关闭流,以保证数据传到bout中

    byte gzip[] = bout.toBtyeArray();
    System.out.println("压缩后的大小:" + data.getBytes().length);

    res.setHeader("Content-Encoding", "gzip");
    res.setHeader("Content-Length", gzip.length + "");
    res.getOutputStream().write(gzip);
}


Content-Language: zh-cn


Content-Type: text/html; charset=GB2312
响应内容的类型格式(在$tomcat/conf/web.xml文件中可以查找相关的格式对应类型)

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("Content-Type", "image/jpeg");
    InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
    int len = 0;
    byte buffer[] = new byte[1024];
    OutputStream out = res.getOutputStream();
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
}


Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT
当前资源的最后访问时间

Refresh: 3;url=http://www.baidu.com
让浏览器每3秒刷新当前页面,如果后面指定的有url则是3秒后跳转到指定url的页面

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("refresh", "3;http://www.baidu.com");
    res.getOutputStream.write("aaaa".getBytes());
}


Content-Disposition: attachment;filename=1.jpg
通知浏览器以下载方式打开数据

public void doGet(HttpServletRequest req, HttpServletResponst res) throws ServleException, IOException {
    res.setHeader("Content-Disposition", "attachment;filename=1.bmp");
    InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
    int len = 0;
    byte buffer[] = new byte[1024];
    OutputStream out = res.getOutputStream();
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
}


Transfer-Encoding: chunked
告诉浏览器数据的传送格式,这里是块传送

Set-Cookie:SS=Q0=5Lb_nQ;path=/search
与缓存相关的头信息

Etag
与缓存相关的头信息,根据web资源内容动态生成的标识字符串,用以标识资源是否发生变化,如果没有,则使用缓存,如果有则使用服务器上的资源
与If-Modified-Since不同,If-Modified-Since是秒级的,即超过1秒才更新,1秒内多次相同请求都取缓存;而Etag是实时的

Expires: -1
告诉浏览器响应数据缓存时间,-1或0不缓存

Cache-Control: no-cache
不进行缓存

Pragma: no-cache
不进行缓存

Connection: close/Keep-Alive


Date: Tue, 11 Jul 2000 18:23:51 GMT

Accept-Ranges: bytes
说明web服务器支持Range

Accept-Ranges: none
说明web服务器不支持Range

Content-Ranges: 1000-3000/5000
指定了返回的web资源的字节范围为1000-3000,总量是5000

你可能感兴趣的:(http请求头,响应头字段)