加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

HttpClient chunked 编码

(2015-05-04 23:06:05)
标签:

httpclient

chunked

我使用的是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: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/ChunkedInputStream.html
------------------------------------------------------------------------------------------------------------------

使用httpclient向服务端发送xml文件,然后服务端经过解析后在发送修改后的xml文件给客户端,现有一问题:当xml文件小的时候一百多K,没有问题,但是当xml为1M时客户端就抛出异常,服务端可以接收到完整的xml内容
异常如下:
Exception in thread "main" org.apache.http.MalformedChunkCodingException: Unexpected content at the end of chunk
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:239)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:204)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:167)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at TestH.main(TestH.java:125)
关键代码:
Java code
?
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, nTimeout);
//写入报文信息
reqEntity.addPart("body"body);
httppost.setEntity(reqEntity);
try {
     response httpclient.execute(httppost);
 catch (IOException e) {
     System.out.println("read data timeout");
     continue;
 
 
   resEntity response.getEntity();
   inputStream resEntity.getContent();
   InputStreamReader reader new InputStreamReader(inputStream ,
   "UTF-8");
   char[] buff new char[1024];
   int length 0;
   while ((length reader.read(buff)) != -1{
      String new String(buff, 0length);
      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{
return readCount;
}
StringEntity entitys = new StringEntity(jsonObject.toString()); entitys.setContentType("application/json"); 
entitys.setChunked(true);



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有