• Http字段含义


    转载自:http://blog.csdn.net/sand_ant/article/details/10503579


    一、request请求Header简介

    Accept:--客户机支持的类型
    Accept-Charset:--采用的编码类型
    Accept-Encoding:--客户机支持的数据压缩格式
    Accept-Language:--客户机语言环境
    Host:--想访问的主机名
    If-Modified-Since:--资源缓存到客户机的时间
    Referer:--跳转来源(跳到此网页所点击的连接,主要用于防盗链)
    User-Agent:--客户机的软件环境(操作系统版本,浏览器版本)
    Cookie:--从客户机传数据
    Connection:--请求完之后是否关闭连接
    Date:--请求时间
    Range:--说明只请求服务器资源的一部分 bytes=1000- 表示获取1000字符之后的数据
     
    [java] view plaincopy
    1. //获取请求资源的url  
    2.  URL url = new URL("http://localhost:8080/day04_web/test.txt");  
    3.  HttpURLConnection con = (HttpURLConnection) url.openConnection();  
    4.  //声明只传输6之后的资源  
    5.  con.setRequestProperty("Range""bytes=6-");  
    6.  //资源写入文件  
    7.  InputStream in = con.getInputStream();  
    8.  FileOutputStream fo = new FileOutputStream("D:\test.txt",true);  
    9.  int len =0;  
    10.  byte[] buff = new byte[1024];  
    11.  while((len=in.read(buff))>0){  
    12.      fo.write(buff, 0, len);  
    13.  }  
    14.  in.close();  
    15.  fo.close();  

    二、response响应Header

    状态行:--处理结果,返回状态码
        //状态码
        100~199 表示请求成功,要求客户机继续提交下一次请求才能完成整个处理过程
        200~299 表示请求成功并完成整个处理过程,常用200
        300~399 为完成请求,客户机需进一步细化请求,例如请求的资源已经移动到一个新地址,
                常用302(请求其他资源,结合location使用)、304(取缓存)、307(取缓存)
        400~499 客户端的请求有错误,常见404(不存在),403(无权限)
        500~599 服务器端出现错误,常见500
        //下面两行代码可以重定向到tt.html中
    [java] view plaincopy
    1. response.setStatus(302);  
    2. response.setHeader("location""tt.html");  

    Server:--服务器类型
    Content-Encoding:--服务器回送数据的压缩格式

    Content-Lengtg:--服务器回送数据的长度

    [java] view plaincopy
    1. String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";  
    2.    System.out.println("===" + data.length());  
    3.    // 通过gzip压缩,压缩结果放到 ByteArrayOutputStream  
    4.    ByteArrayOutputStream out = new ByteArrayOutputStream();  
    5.    GZIPOutputStream gout = new GZIPOutputStream(out);  
    6.    gout.write(data.getBytes());  
    7.    gout.close();  
    8.    byte[] gByte = out.toByteArray();// 压缩后的数据  
    9.    System.out.println("==" + gByte.length);  
    10.    // 通知浏览器数据压缩格式  
    11.    response.setHeader("Content-Encoding""gzip");  
    12.    response.setHeader("Content-Length""" + gByte.length);  
    13.    ServletOutputStream wirte = response.getOutputStream();  
    14.    wirte.write(gByte);  

    Content-Type:--服务器回送数据的类型

    [java] view plaincopy
    1. //设置返回数据的类型  
    2.     response.setHeader("Content-Type""image/x-icon");  
    3.     InputStream in = this.getServletContext().getResourceAsStream("/baidu.ico");  
    4.     ServletOutputStream write = response.getOutputStream();  
    5.     int len = 0;  
    6.     byte buff[] = new byte[1024];  
    7.     while((len = in.read(buff))>0){  
    8.         write.write(buff, 0, len);  
    9.     }  
    10.     in.close();  
    11.     write.flush();  
    12.     write.close();  

    Last-Modified:--当前资源的最后缓存时间
    Refresh:--告诉浏览器多长时间刷新一次
    [java] view plaincopy
    1. //10秒时候跳到百度  
    2.  response.setHeader("refresh""10;url='http://www.baidu.com'");  
    3.  String data ="Search !";  
    4.  response.getOutputStream().write(data.getBytes());  
    Content-Disposition:--告诉浏览器以下载方式打开数据(例如:attachment;filename=3.ico)
    Transfer-Encoding:--告诉浏览器数据的传送格式
    Set-Cookie:--
    ETag:--缓存相关的
    Expires:--告诉浏览器把会送的资源缓存多长时间(0、-1表示不缓存)
    Catch-Control:no-catche --告诉浏览器不要缓存数据
    Pragma:no-catch --告诉浏览器不要缓存数据
  • 相关阅读:
    弹出层layer的使用
    SQL Server SQL分页查询
    C#过滤html标签
    SQLServer ForXmlPath应用
    js调用soapWebService服务
    MediaWiki使用指南
    阿里云金融云服务器配置
    VS无法启动 IISExpress web 服务器
    mysql服务突然丢失解决方案
    [k8s]通过openssl生成证书
  • 原文地址:https://www.cnblogs.com/ycpanda/p/3637187.html
Copyright © 2020-2023  润新知