List of usage examples for java.net HttpURLConnection getContentEncoding
public String getContentEncoding()
From source file:org.apache.flink.runtime.webmonitor.history.HistoryServerTest.java
public static String getFromHTTP(String url) throws Exception { URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setConnectTimeout(100000); connection.connect();/*w w w.j a v a 2s. c o m*/ InputStream is; if (connection.getResponseCode() >= 400) { // error! is = connection.getErrorStream(); } else { is = connection.getInputStream(); } return IOUtils.toString(is, connection.getContentEncoding() != null ? connection.getContentEncoding() : "UTF-8"); }
From source file:cn.garymb.wechatmoments.common.OkHttpStack.java
@SuppressWarnings("deprecation") private static HttpEntity entityFromConnection(HttpURLConnection connection) { BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream;//from ww w.java 2 s. c o m 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; }
From source file:com.kubeiwu.commontool.khttp.toolbox.HurlStack.java
/** * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}. * // w w w.j ava 2 s .co m * @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; }
From source file:com.android.volley.toolbox.HurlStack.java
/** * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}. * @param connection//from w ww. j a v a 2s . c om * @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; }
From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java
/** * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}. * @param connection/*from w ww. ja v a2 s. com*/ * @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; }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
/** * Initializes an {@link HttpEntity} from the given * {@link HttpURLConnection}.//from www . j a va 2s . c o m * * @param connection * @return an HttpEntity populated with data from <code>connection</code>. */ private static HttpEntity entityFromConnection(final HttpURLConnection connection) { final BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream; try { inputStream = connection.getInputStream(); } catch (final IOException ioe) { inputStream = connection.getErrorStream(); } entity.setContent(inputStream); entity.setContentLength(connection.getContentLength()); entity.setContentEncoding(connection.getContentEncoding()); entity.setContentType(connection.getContentType()); return entity; }
From source file:com.evilisn.DAO.CertMapper.java
public static byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception { InputStream is = null;// w ww.j a v a2 s .c om InputStream is_temp = null; try { if (uri == null) return null; URL url = uri.toURL(); if (bActiveCheckUnknownHost) { url.getProtocol(); String host = url.getHost(); int port = url.getPort(); if (port == -1) port = url.getDefaultPort(); InetSocketAddress isa = new InetSocketAddress(host, port); if (isa.isUnresolved()) { //fix JNLP popup error issue throw new UnknownHostException("Host Unknown:" + isa.toString()); } } HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setDoInput(true); uc.setAllowUserInteraction(false); uc.setInstanceFollowRedirects(true); setTimeout(uc); String contentEncoding = uc.getContentEncoding(); int len = uc.getContentLength(); // is = uc.getInputStream(); if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1) { is_temp = uc.getInputStream(); is = new GZIPInputStream(is_temp); } else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1) { is_temp = uc.getInputStream(); is = new InflaterInputStream(is_temp); } else { is = uc.getInputStream(); } if (len != -1) { int ch = 0, i = 0; byte[] res = new byte[len]; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); } return res; } else { ArrayList<byte[]> buffer = new ArrayList<byte[]>(); int buf_len = 1024; byte[] res = new byte[buf_len]; int ch = 0, i = 0; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); if (i == buf_len) { //rotate buffer.add(res); i = 0; res = new byte[buf_len]; } } int total_len = buffer.size() * buf_len + i; byte[] buf = new byte[total_len]; for (int j = 0; j < buffer.size(); j++) { System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len); } if (i > 0) { System.arraycopy(res, 0, buf, buffer.size() * buf_len, i); } return buf; } } catch (Exception e) { e.printStackTrace(); return null; } finally { closeInputStream(is_temp); closeInputStream(is); } }
From source file:org.immopoly.android.helper.WebHelper.java
public static JSONObject getHttpData(URL url, boolean signed, Context context) throws JSONException { JSONObject obj = null;/*from ww w. j a v a 2 s. co m*/ if (Settings.isOnline(context)) { HttpURLConnection request; try { request = (HttpURLConnection) url.openConnection(); request.addRequestProperty("User-Agent", "immopoly android client " + ImmopolyActivity.getStaticVersionInfo()); request.addRequestProperty("Accept-Encoding", "gzip"); if (signed) OAuthData.getInstance(context).consumer.sign(request); request.setConnectTimeout(SOCKET_TIMEOUT); request.connect(); String encoding = request.getContentEncoding(); InputStream in; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { in = new GZIPInputStream(request.getInputStream()); } else { in = new BufferedInputStream(request.getInputStream()); } String s = readInputStream(in); JSONTokener tokener = new JSONTokener(s); obj = new JSONObject(tokener); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (OAuthMessageSignerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (OAuthExpectationFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (OAuthCommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return obj; }
From source file:Main.java
public static String[] getUrlInfos(String urlAsString, int timeout) { try {/*www. ja v a 2s. c o m*/ URL url = new URL(urlAsString); //using proxy may increase latency HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); hConn.setRequestProperty("User-Agent", "Mozilla/5.0 Gecko/20100915 Firefox/3.6.10"); // on android we got problems because of this // so disable that for now // hConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); hConn.setConnectTimeout(timeout); hConn.setReadTimeout(timeout); // default length of bufferedinputstream is 8k byte[] arr = new byte[K4]; InputStream is = hConn.getInputStream(); if ("gzip".equals(hConn.getContentEncoding())) is = new GZIPInputStream(is); BufferedInputStream in = new BufferedInputStream(is, arr.length); in.read(arr); return getUrlInfosFromText(arr, hConn.getContentType()); } catch (Exception ex) { } return new String[] { "", "" }; }
From source file:org.owasp.dependencycheck.utils.Downloader.java
/** * Retrieves a file from a given URL and saves it to the outputPath. * * @param url the URL of the file to download * @param outputPath the path to the save the file to * @param useProxy whether to use the configured proxy when downloading files * @throws DownloadFailedException is thrown if there is an error downloading the file *//*from w w w . j a v a 2 s. c om*/ public static void fetchFile(URL url, File outputPath, boolean useProxy) throws DownloadFailedException { if ("file".equalsIgnoreCase(url.getProtocol())) { File file; try { file = new File(url.toURI()); } catch (URISyntaxException ex) { final String msg = String.format("Download failed, unable to locate '%s'", url.toString()); throw new DownloadFailedException(msg); } if (file.exists()) { try { org.apache.commons.io.FileUtils.copyFile(file, outputPath); } catch (IOException ex) { final String msg = String.format("Download failed, unable to copy '%s'", url.toString()); throw new DownloadFailedException(msg); } } else { final String msg = String.format("Download failed, file does not exist '%s'", url.toString()); throw new DownloadFailedException(msg); } } else { HttpURLConnection conn = null; try { conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.connect(); } catch (IOException ex) { try { if (conn != null) { conn.disconnect(); } } finally { conn = null; } throw new DownloadFailedException("Error downloading file.", ex); } final String encoding = conn.getContentEncoding(); BufferedOutputStream writer = null; InputStream reader = null; try { if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { reader = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) { reader = new InflaterInputStream(conn.getInputStream()); } else { reader = conn.getInputStream(); } writer = new BufferedOutputStream(new FileOutputStream(outputPath)); final byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, bytesRead); } } catch (Throwable ex) { throw new DownloadFailedException("Error saving downloaded file.", ex); } finally { if (writer != null) { try { writer.close(); } catch (Throwable ex) { LOGGER.log(Level.FINEST, "Error closing the writer in Downloader.", ex); } } if (reader != null) { try { reader.close(); } catch (Throwable ex) { LOGGER.log(Level.FINEST, "Error closing the reader in Downloader.", ex); } } try { conn.disconnect(); } finally { conn = null; } } } }