一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - http協議進階之Transfer-Encoding和HttpCore實現詳解

http協議進階之Transfer-Encoding和HttpCore實現詳解

2020-09-20 13:48海鳥 JAVA教程

這篇文章主要給大家介紹了http協議之Transfer-Encoding和HttpCore實現的相關資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

Transfer-Encoding簡介

transfer-eccoding所描述的是消息請求(request)和響應(response)所附帶的實體對象(entity)的傳輸形式,規范定義格式如下:

?
1
Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding 

舉個例子:Transfer-Encoding: chunked

transfer-encoding的可選值有:chunked,identity ;

transfer-encoding的可選值有:chunked,identity,從字面意義可以理解,前者指把要發送傳輸的數據切割成一系列的塊數據傳輸,后者指傳輸時不做任何處理,自身的本質數據形式傳輸。舉個例子,如果我們要傳輸一本“紅樓夢”小說到服務器,chunked方式就會先把這本小說分成一章一章的,然后逐個章節上傳,而identity方式則是從小說的第一個字按順序傳輸到最后一個字結束。

相關的頭定義

Content-Encoding : content-encoding和transfer-encoding所作用的對象不同,行為目標也不同,前者是對數據內容采用什么樣的編碼方式,后者是對數據傳輸采用什么樣的編碼。前者通常是對數據內容進行一些壓縮編碼操作,后者通常是對傳傳輸采用分塊策略之類的。

Content-length : content-length頭的作用是指定待傳輸的內容的字節長度。比如上面舉的例子中,我們要上傳一本紅樓夢小說,則可以指定其長度大小,如:content-length:731017。細心的讀者可能會有疑惑,它和transfer-encoding又有什么關系呢?如果想知道它們的關系,只要反過來問下自己,為什么transfer-encoding會有identity和chunked兩種,各在什么上下文情景中要用到。比如chunked方式,把數據分塊傳輸在很多地方就非常有用,如服務端在處理一個復雜的問題時,其返回結果是階段性的產出,不能一次性知道最終的返回的總長度(content-lenght值),所以這時候返回頭中就不能有content-lenght頭信息,有也要忽略處理。所以你可以這樣理解,transfer-encoding在不能一次性確定消息實體(entity)內容時自定義一些傳輸協議,如果能確定的話,則可以在消息頭中加入content-length頭信息指示其長度,可以把transfer-encoding和content-length看成互斥性的兩種頭。

transfer-encoding詳解

chunked格式(rfc2616 3.6.1):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Chunked-Body = *chunk
          last-chunk
          trailer
          CRLF
chunk  = chunk-size [ chunk-extension ] CRLF
          chunk-data CRLF
chunk-size = 1*HEX
last-chunk = 1*("0") [ chunk-extension ] CRLF
chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
chunk-data = chunk-size(OCTET)
trailer = *(entity-header CRLF)

還是以上傳“紅樓夢”這本書舉例:

http協議進階之Transfer-Encoding和HttpCore實現詳解

24E5是指第一個塊數據長度為24E5(16進制格式字符串表示),CRLF為換行控制符。緊接著是第一個塊數據內容,其長度就是上面定義的24E5,以CRLF標志結束。3485是指第二塊數據長度為3485,CRLF結束,然后后面是第二塊的數據內容......,以這樣的格式直到所有的塊數據結束。最后以“0”CRLF結束,表示數據傳輸完成(這里對比rfc規范內容,省略了chunk-extension和trailer的東西,因為這并不重要)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class Main {
 
 /**
  * @param args
  */
 
 public static final int CR = 13; // <US-ASCII CR, carriage return (13)>
 public static final int LF = 10; // <US-ASCII LF, linefeed (10)>
 
 public static void main(String[] args) throws Exception{
  Socket socket = new Socket("localhost",8080);
  OutputStream out = socket.getOutputStream();
  InputStream in = socket.getInputStream();
  
  //send requestline
  out.write("POST /web/Hello HTTP/1.1".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  
  //send request header
  out.write("Host:localhost:8080".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  out.write("Accept-Encoding:gzip,deflate".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  out.write("Transfer-Encoding:chunked".getBytes());// 指定transfer-encodeing為chunked方式
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  out.write("Content-Type:application/x-www-form-urlencoded;charset=utf-8".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  
  // CRLF between headers and entity
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  
  /*
   * send chunked data
   */
  //send the first chunked data:hello,world
  //the first chunked data's size : 11
  out.write("B".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  //the first chunked data's content : hello,world
  out.write("hello,world".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  //send the second chunked data:tony
  //the first chunked data's size : 4
  out.write("4".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  //the first chunked data's content : hello,world
  out.write("tony".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  //send the chunked data end flag
  out.write("0".getBytes());
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  //send CRLF
  out.write(CR & 0xFF);
  out.write(LF & 0xFF);
  
  out.flush();
  
  //
  byte[] buffer = new byte[512];
  ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
  int len = -1;
  while((len = in.read(buffer)) != -1){
   bufferStream.write(buffer,0,len);
  }
  
  System.out.println(new String(bufferStream.toByteArray()));
  
  socket.close();
   
 }

上面這段代碼發了兩塊數據,第一塊是“hello,world”這11個字節長度的字符,第二塊發送了“tony”四個字長的數據塊。在服務端將收到“hello,worldtony”這個字符串.

HttpCore對transfer-encoding的實現

http協議進階之Transfer-Encoding和HttpCore實現詳解

所以不管是對輸入流(InputStream),還是輸出流(OutputStream),httpcore都有三種實現:contentlength,identity,chunked。這是完全按照http規范實現的。這里再重復總結下這三種這間的關系。

當指定了"content-length"頭信息時,說明已經確定消息體(entity)的長度大小,其值必需為非負整數。反之,如果有“transfer-encoding”頭信息時,其值為“chunked”或者“identity”,說明不確定消息體的大小,這時應該不存在“content-length”頭。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/jcli/archive/2012/10/19/2730440.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 小小水蜜桃视频高清在线播放 | 四虎新网址 | 香蕉视频在线观看网址 | 911精品国产亚洲日本美国韩国 | 男gay男gay男gay野外 | 国产精品一区二区三区免费 | 亚洲精品丝袜在线一区波多野结衣 | 2020精品极品国产色在线观看 | 色综合久久中文字幕综合网 | 成人精品网 | 成人男女啪啪免费观看网站 | 国产精品久久现线拍久青草 | 福利片成人午夜在线 | 91免费精品国自产拍在线不卡 | 成人网欧美亚洲影视图片 | 99久久成人 | 日韩亚洲人成在线综合 | 国产乱妇无码大片在线观看 | fuqer日本老师 | 日韩中文字幕视频在线观看 | 欧美日韩亚洲综合在线一区二区 | 男人的天堂久久爱 | 色老板最新网站视频地址 | 亚洲精品乱码久久久久久蜜桃 | 免费观看日本视频 | 男人和女人全黄一级毛片 | 午夜精品久久久久久 | 精品一区二区视频 | 国产午夜精品理论片 | 911精品国产亚洲日本美国韩国 | 国内精品九一在线播放 | 黄+色+性+人免费 | 免费人成在线观看69式小视频 | 俄罗斯freeoo性另类 | 久久精品国产亚洲AV热无遮挡 | 久久精品国产欧美日韩99热 | 色综合久久中文字幕 | 我把校花黑色蕾丝胸罩脱了 | 成人看片免费无限观看视频 | 亚洲sss综合天堂久久久 | 视频一区二区三区在线观看 |