亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

http協(xié)議進(jìn)階之Transfer-Encoding和HttpCore實(shí)現(xiàn)詳解

 更新時(shí)間:2017年04月27日 10:16:39   作者:海鳥  
這篇文章主要給大家介紹了http協(xié)議之Transfer-Encoding和HttpCore實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。

Transfer-Encoding簡(jiǎn)介

transfer-eccoding所描述的是消息請(qǐng)求(request)和響應(yīng)(response)所附帶的實(shí)體對(duì)象(entity)的傳輸形式,規(guī)范定義格式如下:

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

舉個(gè)例子:Transfer-Encoding: chunked

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

transfer-encoding的可選值有:chunked,identity,從字面意義可以理解,前者指把要發(fā)送傳輸?shù)臄?shù)據(jù)切割成一系列的塊數(shù)據(jù)傳輸,后者指?jìng)鬏敃r(shí)不做任何處理,自身的本質(zhì)數(shù)據(jù)形式傳輸。舉個(gè)例子,如果我們要傳輸一本“紅樓夢(mèng)”小說(shuō)到服務(wù)器,chunked方式就會(huì)先把這本小說(shuō)分成一章一章的,然后逐個(gè)章節(jié)上傳,而identity方式則是從小說(shuō)的第一個(gè)字按順序傳輸?shù)阶詈笠粋€(gè)字結(jié)束。

相關(guān)的頭定義

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

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

transfer-encoding詳解

chunked格式(rfc2616 3.6.1):

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)

還是以上傳“紅樓夢(mèng)”這本書舉例:

24E5是指第一個(gè)塊數(shù)據(jù)長(zhǎng)度為24E5(16進(jìn)制格式字符串表示),CRLF為換行控制符。緊接著是第一個(gè)塊數(shù)據(jù)內(nèi)容,其長(zhǎng)度就是上面定義的24E5,以CRLF標(biāo)志結(jié)束。3485是指第二塊數(shù)據(jù)長(zhǎng)度為3485,CRLF結(jié)束,然后后面是第二塊的數(shù)據(jù)內(nèi)容......,以這樣的格式直到所有的塊數(shù)據(jù)結(jié)束。最后以“0”CRLF結(jié)束,表示數(shù)據(jù)傳輸完成(這里對(duì)比rfc規(guī)范內(nèi)容,省略了chunk-extension和trailer的東西,因?yàn)檫@并不重要)。

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();
   
 }

上面這段代碼發(fā)了兩塊數(shù)據(jù),第一塊是“hello,world”這11個(gè)字節(jié)長(zhǎng)度的字符,第二塊發(fā)送了“tony”四個(gè)字長(zhǎng)的數(shù)據(jù)塊。在服務(wù)端將收到“hello,worldtony”這個(gè)字符串.

HttpCore對(duì)transfer-encoding的實(shí)現(xiàn)

所以不管是對(duì)輸入流(InputStream),還是輸出流(OutputStream),httpcore都有三種實(shí)現(xiàn):contentlength,identity,chunked。這是完全按照http規(guī)范實(shí)現(xiàn)的。這里再重復(fù)總結(jié)下這三種這間的關(guān)系。

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論