private static HttpEntity entityFromOkHttpResponse(Response response) throws IOException { BasicHttpEntity entity = new BasicHttpEntity(); ResponseBody body = response.body(); entity.setContent(body.byteStream()); entity.setContentLength(body.contentLength()); entity.setContentEncoding(response.header("Content-Encoding")); if (body.contentType() != null) { entity.setContentType(body.contentType().type()); } return entity; }
/** * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}. * * @param connection * @return an HttpEntity populated with data from <code>connection</code>. */ private static HttpEntity entityFromConnection(HttpURLConnection connection) { BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream; try { inputStream = connection.getInputStream(); } catch (IOException ioe) { inputStream = connection.getErrorStream(); } entity.setContent(inputStream); entity.setContentLength(connection.getContentLength()); entity.setContentEncoding(connection.getContentEncoding()); entity.setContentType(connection.getContentType()); return entity; }
private static HttpEntity a(HttpURLConnection httpurlconnection) { BasicHttpEntity basichttpentity = new BasicHttpEntity(); java.io.InputStream inputstream; try { inputstream = httpurlconnection.getInputStream(); } catch (IOException ioexception) { ioexception = httpurlconnection.getErrorStream(); } basichttpentity.setContent(inputstream); basichttpentity.setContentLength(httpurlconnection.getContentLength()); basichttpentity.setContentEncoding(httpurlconnection.getContentEncoding()); basichttpentity.setContentType(httpurlconnection.getContentType()); return basichttpentity; }
@Test public void testGetContentEncoding() throws Exception { String input = "0123456789"; BasicHttpEntity basic; PartiallyRepeatableHttpEntity replay; basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.getContentEncoding(), nullValue()); basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); basic.setContentEncoding("UTF-8"); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.getContentEncoding().getValue(), is("UTF-8")); }
/** * 执行HTTP请求之后获取到的数据流. * * @param connection * @return */ private HttpEntity entityFromURLConnwction(HttpURLConnection connection) { BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream; try { inputStream = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); inputStream = connection.getErrorStream(); } // TODO : GZIP entity.setContent(inputStream); entity.setContentLength(connection.getContentLength()); entity.setContentEncoding(connection.getContentEncoding()); entity.setContentType(connection.getContentType()); return entity; }