List of usage examples for java.net URLConnection getContentEncoding
public String getContentEncoding()
From source file:org.exoplatform.chat.listener.ServerBootstrap.java
private static String postServer(String serviceUri, String params) { String serviceUrl = getServerBase() + PropertyManager.getProperty(PropertyManager.PROPERTY_CHAT_SERVER_URL) + "/" + serviceUri; String allParams = "passphrase=" + PropertyManager.getProperty(PropertyManager.PROPERTY_PASSPHRASE) + "&" + params;/*from www .j av a2 s . co m*/ String body = ""; OutputStreamWriter writer = null; try { URL url = new URL(serviceUrl); URLConnection con = url.openConnection(); con.setDoOutput(true); //envoi de la requte writer = new OutputStreamWriter(con.getOutputStream()); writer.write(allParams); writer.flush(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; body = IOUtils.toString(in, encoding); if ("null".equals(body)) body = null; } catch (MalformedURLException e) { LOG.warning(e.getMessage()); } catch (IOException e) { LOG.warning(e.getMessage()); } finally { try { writer.close(); } catch (Exception e) { LOG.warning(e.getMessage()); } } return body; }
From source file:io.jari.geenstijl.API.API.java
static String downloadString(String url) throws IOException { if (url.startsWith("//")) { url = "http:" + url; } else if (url.startsWith("://")) { url = "http" + url; }// w ww . j a v a2s. c om URLConnection con = new URL(url).openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; int len = 0; while ((len = in.read(buf)) != -1) { baos.write(buf, 0, len); } return new String(baos.toByteArray(), encoding); }
From source file:com.omertron.thetvdbapi.tools.WebBrowser.java
/** * Request the web page at the specified URL * * @param url//from w w w. j a v a 2 s.co m * @throws IOException */ public static String request(URL url) throws IOException { StringBuilder content = new StringBuilder(); BufferedReader in = null; URLConnection cnx = null; InputStreamReader isr = null; GZIPInputStream zis = null; try { cnx = openProxiedConnection(url); sendHeader(cnx); readHeader(cnx); // Check the content encoding of the connection. Null content encoding is standard HTTP if (cnx.getContentEncoding() == null) { //in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx))); isr = new InputStreamReader(cnx.getInputStream(), "UTF-8"); } else if (cnx.getContentEncoding().equalsIgnoreCase("gzip")) { zis = new GZIPInputStream(cnx.getInputStream()); isr = new InputStreamReader(zis, "UTF-8"); } else { return ""; } in = new BufferedReader(isr); String line; while ((line = in.readLine()) != null) { content.append(line); } } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (isr != null) { try { isr.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (zis != null) { try { zis.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (cnx instanceof HttpURLConnection) { ((HttpURLConnection) cnx).disconnect(); } } return content.toString(); }
From source file:ch.njol.skript.Updater.java
/** * Gets the changelogs and release dates of the newest versions * // w w w . ja va 2 s . c o m * @param sender */ final static void getChangelogs(final CommandSender sender) { InputStream in = null; InputStreamReader r = null; try { final URLConnection conn = new URL(RSSURL).openConnection(); conn.setRequestProperty("User-Agent", "Skript/v" + Skript.getVersion() + " (by Njol)"); // Bukkit returns a 403 (forbidden) if no user agent is set in = conn.getInputStream(); r = new InputStreamReader(in, conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding()); final XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(r); infos.clear(); VersionInfo current = null; outer: while (reader.hasNext()) { XMLEvent e = reader.nextEvent(); if (e.isStartElement()) { final String element = e.asStartElement().getName().getLocalPart(); if (element.equalsIgnoreCase("title")) { final String name = reader.nextEvent().asCharacters().getData().trim(); for (final VersionInfo i : infos) { if (name.equals(i.name)) { current = i; continue outer; } } current = null; } else if (element.equalsIgnoreCase("description")) { if (current == null) continue; final StringBuilder cl = new StringBuilder(); while ((e = reader.nextEvent()).isCharacters()) cl.append(e.asCharacters().getData()); current.changelog = "- " + StringEscapeUtils.unescapeHtml("" + cl).replace("<br>", "") .replace("<p>", "").replace("</p>", "").replaceAll("\n(?!\n)", "\n- "); } else if (element.equalsIgnoreCase("pubDate")) { if (current == null) continue; synchronized (RFC2822) { // to make FindBugs shut up current.date = new Date( RFC2822.parse(reader.nextEvent().asCharacters().getData()).getTime()); } } } } } catch (final IOException e) { stateLock.writeLock().lock(); try { state = UpdateState.CHECK_ERROR; error.set(ExceptionUtils.toString(e)); Skript.error(sender, m_check_error.toString()); } finally { stateLock.writeLock().unlock(); } } catch (final Exception e) { Skript.error(sender, m_internal_error.toString()); Skript.exception(e, "Unexpected error while checking for a new version of Skript"); stateLock.writeLock().lock(); try { state = UpdateState.CHECK_ERROR; error.set(e.getClass().getSimpleName() + ": " + e.getLocalizedMessage()); } finally { stateLock.writeLock().unlock(); } } finally { if (in != null) { try { in.close(); } catch (final IOException e) { } } if (r != null) { try { r.close(); } catch (final IOException e) { } } } }
From source file:ch.njol.skript.Updater.java
/** * @param sender Sender to receive messages * @param download Whether to directly download the newest version if one is found * @param isAutomatic/* w ww . j a v a 2 s .co m*/ */ static void check(final CommandSender sender, final boolean download, final boolean isAutomatic) { stateLock.writeLock().lock(); try { if (state == UpdateState.CHECK_IN_PROGRESS || state == UpdateState.DOWNLOAD_IN_PROGRESS) return; state = UpdateState.CHECK_IN_PROGRESS; } finally { stateLock.writeLock().unlock(); } if (!isAutomatic || Skript.logNormal()) Skript.info(sender, "" + m_checking); Skript.newThread(new Runnable() { @Override public void run() { infos.clear(); InputStream in = null; try { final URLConnection conn = new URL(filesURL).openConnection(); conn.setRequestProperty("User-Agent", "Skript/v" + Skript.getVersion() + " (by Njol)"); in = conn.getInputStream(); final BufferedReader reader = new BufferedReader(new InputStreamReader(in, conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding())); try { final String line = reader.readLine(); if (line != null) { final JSONArray a = (JSONArray) JSONValue.parse(line); for (final Object o : a) { final Object name = ((JSONObject) o).get("name"); if (!(name instanceof String) || !((String) name).matches("\\d+\\.\\d+(\\.\\d+)?( \\(jar( only)?\\))?"))// not the default version pattern to not match beta/etc. versions continue; final Object url = ((JSONObject) o).get("downloadUrl"); if (!(url instanceof String)) continue; final Version version = new Version(((String) name).contains(" ") ? "" + ((String) name).substring(0, ((String) name).indexOf(' ')) : ((String) name)); if (version.compareTo(Skript.getVersion()) > 0) { infos.add(new VersionInfo((String) name, version, (String) url)); } } } } finally { reader.close(); } if (!infos.isEmpty()) { Collections.sort(infos); latest.set(infos.get(0)); } else { latest.set(null); } getChangelogs(sender); final String message = infos.isEmpty() ? (Skript.getVersion().isStable() ? "" + m_running_latest_version : "" + m_running_latest_version_beta) : "" + m_update_available; if (isAutomatic && !infos.isEmpty()) { Skript.adminBroadcast(message); } else { Skript.info(sender, message); } if (download && !infos.isEmpty()) { stateLock.writeLock().lock(); try { state = UpdateState.DOWNLOAD_IN_PROGRESS; } finally { stateLock.writeLock().unlock(); } download_i(sender, isAutomatic); } else { stateLock.writeLock().lock(); try { state = UpdateState.CHECKED_FOR_UPDATE; } finally { stateLock.writeLock().unlock(); } } } catch (final IOException e) { stateLock.writeLock().lock(); try { state = UpdateState.CHECK_ERROR; error.set(ExceptionUtils.toString(e)); if (sender != null) Skript.error(sender, m_check_error.toString()); } finally { stateLock.writeLock().unlock(); } } catch (final Exception e) { if (sender != null) Skript.error(sender, m_internal_error.toString()); Skript.exception(e, "Unexpected error while checking for a new version of Skript"); stateLock.writeLock().lock(); try { state = UpdateState.CHECK_ERROR; error.set(e.getClass().getSimpleName() + ": " + e.getLocalizedMessage()); } finally { stateLock.writeLock().unlock(); } } finally { if (in != null) { try { in.close(); } catch (final IOException e) { } } } } }, "Skript update thread").start(); }
From source file:com.frostwire.gui.library.LibraryUtils.java
private static InternetRadioStation processInternetRadioStationUrl(String urlStr) throws Exception { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setConnectTimeout(10000);//from w w w . j a v a 2 s . co m conn.setRequestProperty("User-Agent", "Java"); InputStream is = conn.getInputStream(); BufferedReader d = null; if (conn.getContentEncoding() != null) { d = new BufferedReader(new InputStreamReader(is, conn.getContentEncoding())); } else { d = new BufferedReader(new InputStreamReader(is)); } String pls = ""; String[] props = null; String strLine; int numLine = 0; while ((strLine = d.readLine()) != null) { pls += strLine + "\n"; if (strLine.startsWith("File1=")) { String streamUrl = strLine.split("=")[1]; props = processStreamUrl(streamUrl); } else if (strLine.startsWith("icy-name:")) { pls = ""; props = processStreamUrl(urlStr); break; } numLine++; if (numLine > 10) { if (props == null) { // not a valid pls break; } } } is.close(); if (props != null && props[0] != null) { return LibraryMediator.getLibrary().newInternetRadioStation(props[0], props[0], props[1], props[2], props[3], props[4], props[5], pls, false); } else { return null; } }
From source file:com.frostwire.gui.library.LibraryUtils.java
private static String[] processStreamUrl(String streamUrl) throws Exception { URL url = new URL(streamUrl); System.out.print(" - " + streamUrl); URLConnection conn = url.openConnection(); conn.setConnectTimeout(10000);/*from w ww.ja v a 2 s . c om*/ conn.setRequestProperty("User-Agent", "Java"); InputStream is = conn.getInputStream(); BufferedReader d = null; if (conn.getContentEncoding() != null) { d = new BufferedReader(new InputStreamReader(is, conn.getContentEncoding())); } else { d = new BufferedReader(new InputStreamReader(is)); } String name = null; String genre = null; String website = null; String type = null; String br = null; String strLine; int i = 0; while ((strLine = d.readLine()) != null && i < 10) { if (strLine.startsWith("icy-name:")) { name = clean(strLine.split(":")[1]); } else if (strLine.startsWith("icy-genre:")) { genre = clean(strLine.split(":")[1]); } else if (strLine.startsWith("icy-url:")) { website = strLine.split("icy-url:")[1].trim(); } else if (strLine.startsWith("content-type:")) { String contentType = strLine.split(":")[1].trim(); if (contentType.equals("audio/aacp")) { type = "AAC+"; } else if (contentType.equals("audio/mpeg")) { type = "MP3"; } else if (contentType.equals("audio/aac")) { type = "AAC"; } } else if (strLine.startsWith("icy-br:")) { br = strLine.split(":")[1].trim() + " kbps"; } i++; } is.close(); return new String[] { name, streamUrl, br, type, website, genre }; }
From source file:bolts.WebViewAppLinkResolver.java
/** * Gets a string with the proper encoding (including using the charset specified in the MIME type * of the request) from a URLConnection. *//*from ww w . j a v a 2 s . com*/ private static String readFromConnection(URLConnection connection) throws IOException { InputStream stream; if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; try { stream = connection.getInputStream(); } catch (Exception e) { stream = httpConnection.getErrorStream(); } } else { stream = connection.getInputStream(); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read = 0; while ((read = stream.read(buffer)) != -1) { output.write(buffer, 0, read); } String charset = connection.getContentEncoding(); if (charset == null) { String mimeType = connection.getContentType(); String[] parts = mimeType.split(";"); for (String part : parts) { part = part.trim(); if (part.startsWith("charset=")) { charset = part.substring("charset=".length()); break; } } if (charset == null) { charset = "UTF-8"; } } return new String(output.toByteArray(), charset); } finally { stream.close(); } }
From source file:com.adobe.phonegap.contentsync.Sync.java
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException { String encoding = conn.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("gzip")) { return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream())); }/*from w ww .j a v a 2 s . co m*/ return new SimpleTrackingInputStream(conn.getInputStream()); }
From source file:com.sxit.crawler.utils.ArchiveUtils.java
/** * Get a BufferedReader on the crawler journal given. * /*w ww. j a va 2 s . c o m*/ * @param source URL journal * @return journal buffered reader. * @throws IOException */ public static BufferedReader getBufferedReader(URL source) throws IOException { URLConnection conn = source.openConnection(); boolean isGzipped = conn.getContentType() != null && conn.getContentType().equalsIgnoreCase("application/x-gzip") || conn.getContentEncoding() != null && conn.getContentEncoding().equalsIgnoreCase("gzip"); InputStream uis = conn.getInputStream(); return new BufferedReader( isGzipped ? new InputStreamReader(new GZIPInputStream(uis)) : new InputStreamReader(uis)); }