Chunked transfer encoding 简单说明

本文详细解析了Chunked Transfer Encoding的工作原理,并通过Java代码实例展示了如何实现该编码方式,包括编码响应和解码数据的过程。

Chunked transfer encoding  在协议总是很重要的,

基本分为2部分,一部分是POST checke 一部分是down

 

下面介绍InputStream那部分

 

 

Sample

Encoded response
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1A
and this is the second one
0

same as above, raw bytes in hex
0000-000F   48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d   HTTP/1.1 200 OK.
0010-001F   0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74   .Content-Type: t
0020-002F   65 78 74 2f 70 6c 61 69 6e 0d 0a 54 72 61 6e 73   ext/plain..Trans
0030-003F   66 65 72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68   fer-Encoding: ch
0040-004F   75 6e 6b 65 64 0d 0a 0d 0a 32 35 0d 0a 54 68 69   unked....25..Thi
0050-005F   73 20 69 73 20 74 68 65 20 64 61 74 61 20 69 6e   s is the data in
0060-006F   20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b    the first chunk
0070-007F   0d 0a 0d 0a 31 41 0d 0a 61 6e 64 20 74 68 69 73   ....1A..and this
0080-008F   20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f    is the second o
0090-009F   6e 65 0d 0a 30 0d 0a 0d 0a                        ne..0....
same as above, in Java code
public static final byte[] CHUNKED_RESPONSE;
static {		 
	StringBuilder sb = new StringBuilder();
	sb.append("HTTP/1.1 200 OK\r\n");
	sb.append("Content-Type: text/plain\r\n");
	sb.append("Transfer-Encoding: chunked\r\n\r\n");
	sb.append("25\r\n");		
	sb.append("This is the data in the first chunk\r\n"); // 37 bytes of payload
			// (conveniently consisting of ASCII characters only)
	sb.append("\r\n1A\r\n");
	sb.append("and this is the second one"); // 26 bytes of payload
			// (conveniently consisting of ASCII characters only)
	sb.append("\r\n0\r\n\r\n");
	CHUNKED_RESPONSE = sb.toString().getBytes(java.nio.charset.Charset.forName("US-ASCII"));
}

Decoded data
This is the data in the first chunk
and this is the second one

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值