@Test
 public void testPrepareInputIdentity() throws Exception {
   final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
   final HttpEntity entity = conn.prepareInput(message);
   Assert.assertNotNull(entity);
   Assert.assertFalse(entity.isChunked());
   Assert.assertEquals(-1, entity.getContentLength());
   final InputStream instream = entity.getContent();
   Assert.assertNotNull(instream);
   Assert.assertTrue((instream instanceof IdentityInputStream));
 }
 /**
  * 请求转换成汉字
  *
  * @param charset
  * @param httpResponse
  * @return
  * @throws IOException
  */
 protected String getContent(String charset, HttpResponse httpResponse) throws IOException {
   if ("".equals(charset) || charset == null) { // 没有传递编码
     HttpEntity entity = httpResponse.getEntity();
     byte[] byteContent = null;
     if (entity.isChunked()) {
       BufferedInputStream remoteBIS = new BufferedInputStream(entity.getContent());
       ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
       byte[] buf = new byte[1024];
       int bytesRead = 0;
       while (bytesRead >= 0) {
         baos.write(buf, 0, bytesRead);
         try {
           bytesRead = remoteBIS.read(buf);
         } catch (IOException ex) {
           remoteBIS.close();
           log.warn("chunked");
           break;
         }
       }
       byteContent = baos.toByteArray();
       baos.close();
     } else {
       byteContent = EntityUtils.toByteArray(entity);
     }
     Header header = httpResponse.getEntity().getContentType();
     // charset
     // 1、encoding in http header Content-Type
     String ContentType = "";
     if (header != null) {
       ContentType = header.getValue();
     }
     String htmlCharset = getHtmlCharset(ContentType, byteContent);
     log.info("charset is " + htmlCharset);
     if (htmlCharset != null) {
       setCharset(charset);
       return new String(byteContent, htmlCharset);
     } else {
       log.warn("Charset autodetect failed, use utf-8 as charset.");
       return new String(byteContent, "utf-8");
     }
   } else { // 如果已经传递编码,那么使用传递的
     setCharset(charset);
     return IOUtils.toString(httpResponse.getEntity().getContent(), charset);
   }
 }
 @Test
 public void testPrepareInputLengthDelimited() throws Exception {
   final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
   message.addHeader("Content-Length", "10");
   message.addHeader("Content-Type", "stuff");
   message.addHeader("Content-Encoding", "identity");
   final HttpEntity entity = conn.prepareInput(message);
   Assert.assertNotNull(entity);
   Assert.assertFalse(entity.isChunked());
   Assert.assertEquals(10, entity.getContentLength());
   final Header ct = entity.getContentType();
   Assert.assertNotNull(ct);
   Assert.assertEquals("stuff", ct.getValue());
   final Header ce = entity.getContentEncoding();
   Assert.assertNotNull(ce);
   Assert.assertEquals("identity", ce.getValue());
   final InputStream instream = entity.getContent();
   Assert.assertNotNull(instream);
   Assert.assertTrue((instream instanceof ContentLengthInputStream));
 }
 @Override
 public boolean isChunked() {
   return delegate.isChunked();
 }
 @Override
 public boolean isChunked() {
   return src.isChunked();
 }
Esempio n. 6
0
 @Override
 public boolean isChunked() {
   return httpEntity.isChunked();
 }