Http字段含義

一、request請求Header簡介

Accept:--客戶機支持的類型
Accept-Charset:--採用的編碼類型
Accept-Encoding:--客戶機支持的數據壓縮格式
Accept-Language:--客戶機語言環境
Host:--想訪問的主機名
If-Modified-Since:--資源緩存到客戶機的時間
Referer:--跳轉來源(跳到此網頁所點擊的連接,主要用於防盜鏈)
User-Agent:--客戶機的軟件環境(操作系統版本,瀏覽器版本)
Cookie:--從客戶機傳數據
Connection:--請求完之後是否關閉連接
Date:--請求時間
Range:--說明只請求服務器資源的一部分 bytes=1000- 表示獲取1000字符之後的數據
 
   //獲取請求資源的url
    URL url = new URL("http://localhost:8080/day04_web/test.txt");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    //聲明只傳輸6之後的資源
    con.setRequestProperty("Range", "bytes=6-");
    //資源寫入文件
    InputStream in = con.getInputStream();
    FileOutputStream fo = new FileOutputStream("D:\\test.txt",true);
    int len =0;
    byte[] buff = new byte[1024];
    while((len=in.read(buff))>0){
        fo.write(buff, 0, len);
    }
    in.close();
    fo.close();

二、response響應Header

狀態行:--處理結果,返回狀態碼
    //狀態碼
    100~199 表示請求成功,要求客戶機繼續提交下一次請求才能完成整個處理過程
    200~299 表示請求成功並完成整個處理過程,常用200
    300~399 爲完成請求,客戶機需進一步細化請求,例如請求的資源已經移動到一個新地址,
            常用302(請求其他資源,結合location使用)、304(取緩存)、307(取緩存)
    400~499 客戶端的請求有錯誤,常見404(不存在),403(無權限)
    500~599 服務器端出現錯誤,常見500
    //下面兩行代碼可以重定向到tt.html中
    response.setStatus(302);
    response.setHeader("location", "tt.html");

Server:--服務器類型
Content-Encoding:--服務器回送數據的壓縮格式

Content-Lengtg:--服務器回送數據的長度

 String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    System.out.println("===" + data.length());
    // 通過gzip壓縮,壓縮結果放到 ByteArrayOutputStream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(out);
    gout.write(data.getBytes());
    gout.close();
    byte[] gByte = out.toByteArray();// 壓縮後的數據
    System.out.println("==" + gByte.length);
    // 通知瀏覽器數據壓縮格式
    response.setHeader("Content-Encoding", "gzip");
    response.setHeader("Content-Length", "" + gByte.length);
    ServletOutputStream wirte = response.getOutputStream();
    wirte.write(gByte);

Content-Type:--服務器回送數據的類型

//設置返回數據的類型
    response.setHeader("Content-Type", "image/x-icon");
    InputStream in = this.getServletContext().getResourceAsStream("/baidu.ico");
    ServletOutputStream write = response.getOutputStream();
    int len = 0;
    byte buff[] = new byte[1024];
    while((len = in.read(buff))>0){
        write.write(buff, 0, len);
    }
    in.close();
    write.flush();
    write.close();
Last-Modified:--當前資源的最後緩存時間
Refresh:--告訴瀏覽器多長時間刷新一次
   //10秒時候跳到百度
    response.setHeader("refresh", "10;url='http://www.baidu.com'");
    String data ="Search !";
    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 --告訴瀏覽器不要緩存數據
發佈了24 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章