HttpClient chunked 编码
(2015-05-04 23:06:05)
标签:
httpclientchunked |
我使用的是apache httpclient 4.2
请求某个url后,response是gzip + chunk
但是解压后,好像总是丢包(chunk是分多包的)
respnose:
...
Transfer-Encoding: chunked
...
Content-Encoding: gzip
...
g-zip流解码代码:
response.setEntity(new
GzipDecompressingEntity(response.getEntity()));
问题补充:问题已解决,对于chunk处理,不要在httpPost.releaseConnection()之后再去获取流resp.getEntity().getContent()并处理。
我这边的验证是有效的。
我这边的验证是有效的。
HttpClient 支持chunked编码 需要注意的是
httpClient.executeMethod(postMethod);执行后并不会抓取ResponseBody 必须
System.out.println(postMethod.getResponseBodyAsString());getResponseBody(); API DOC:
------------------------------------------------------------------------------------------------------------------
使用httpclient向服务端发送xml文件,然后服务端经过解析后在发送修改后的xml文件给客户端,现有一问题:当xml文件小的时候一百多K,没有问题,但是当xml为1M时客户端就抛出异常,服务端可以接收到完整的xml内容
异常如下:
Exceptionin thread "main" org.apache.http.MalformedChunkCodingException: Unexpected content at the end of chunk
atorg.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:239)
atorg.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:204)
atorg.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:167)
atorg.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
atsun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
atsun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source
atsun.nio.cs.StreamDecoder.read(Unknown Source)
atjava.io.InputStreamReader.read(Unknown Source)
atjava.io.Reader.read(Unknown Source)
atTestH.main(TestH.java:125)
关键代码:
StringEntity entitys = new StringEntity(jsonObject.toString());
entitys.setContentType("application/json"); 异常如下:
Exception
at
at
at
at
at
at
at
at
at
at
关键代码:
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
|
httppost new HttpPost(URL);
//请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
nTimeout);
//读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
//写入报文信息
reqEntity.addPart( "body" ,
httppost.setEntity(reqEntity);
try {
response
} catch (IOException
System.out.println( "read );
continue ;
}
resEntity
inputStream
InputStreamReader new InputStreamReader(inputStream
"UTF-8" );
char [] new char [ 1024 ];
int length 0 ;
while ((length 1 )
String new String(buff, 0 ,
System.out.println(x);
}
|
int readCount = innerReader.ReadContentAsBase64(buffer, index, count);
if(readCount == 0) {
//GetReader
From
NextChunk will wait for chunks to be queued
//then set this.innerReader to the received message's reader
//it will throw TimeoutException if the next chunk is not received within specified timeout
Get Reader From Next
Chunk(receiveTimeout);
if (! isLastChunk){
readCount = innerReader.ReadContentAsBase64(buffer, index, count);
if (readCount == 0) {
throw new CommunicationException("Received chunk contains no data");
} else{
return readCount;
}
} else //lastChunk
{
return 0;
}
}else{
returnreadCount;
}
if
}else{
return
}
entitys.setChunked(true);