List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:Main.java
/** * @param http/*from www. j av a 2s .c o m*/ * @return extracted title of page from html. * @throws IOException */ public static String getPageTitle(String linkUrl) throws IOException { URL url = new URL(linkUrl); URLConnection conn = null; try { conn = url.openConnection(); String type = conn.getContentType(); if (type != null && !type.toLowerCase().contains("text/html")) return null; // if not html, we can't get title. Return from here. else { // read response stream BufferedReader bufReader = new BufferedReader( new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); int n = 0; int readCount = 0; char[] buf = new char[1024]; StringBuilder content = new StringBuilder(); // read till end of file or 8 times the buffer (no need to read // all data, assuming we get content in first 8 reads) while (readCount < 8 && (n = bufReader.read(buf, 0, buf.length)) != -1) { content.append(buf, 0, n); readCount++; } bufReader.close(); // extract the title Matcher matcher = HTML_TITLE_PATTERN.matcher(content); if (matcher.find()) { // replace whitespaces and HTML brackets return matcher.group(1).replaceAll("[\\s\\<>]+", " ").trim(); } else return null; } } finally { //any cleanup? } }
From source file:Main.java
public static String[][] getSpreadSheet(String docId, String tab) { try {/*from w w w . j a v a2 s .co m*/ // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) { } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); //sslSocketFactory. // All set up, we can get a resource through https now: final URLConnection urlCon = new URL( "https://docs.google.com/spreadsheets/d/" + docId + "/export?format=csv&gid=" + tab) .openConnection(); // Tell the url connection object to use our socket factory which bypasses security checks ((HttpsURLConnection) urlCon).setSSLSocketFactory(sslSocketFactory); final InputStream input = urlCon.getInputStream(); BufferedReader r = new BufferedReader(new InputStreamReader(input)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); total.append("\n"); } String theString = total.toString(); String[][] out = null; String rows[] = theString.split("\n"); out = new String[rows.length][]; for (int i = 0; i < out.length; i++) { String columns[] = rows[i].split(","); out[i] = new String[columns.length]; int corrected = 0; for (int j = 0; j < columns.length; j++) { if (columns[j].length() > 0 && columns[j].charAt(0) == '"') { out[i][j - corrected] = (columns[j] + ", " + columns[j + 1]).replace("\"", ""); j++; corrected += 1; } else { out[i][j - corrected] = columns[j]; } } } return out; } catch (final Exception e) { e.printStackTrace(); } return null; }
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; }//from w w w . j a va2s . co m 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:org.alfresco.webservice.util.ContentUtils.java
/** * Get the content from the download servlet as a string * // w ww .j a v a 2s. co m * @param content the content object * @return the content as a string */ public static String getContentAsString(Content content) { // Get the url and the ticket String ticket = AuthenticationUtils.getTicket(); String strUrl = content.getUrl() + "?ticket=" + ticket; StringBuilder readContent = new StringBuilder(); try { // Connect to donwload servlet URL url = new URL(strUrl); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", "JSESSIONID=" + AuthenticationUtils.getAuthenticationDetails().getSessionId() + ";"); InputStream is = conn.getInputStream(); int read = is.read(); while (read != -1) { readContent.append((char) read); read = is.read(); } } catch (Exception exception) { throw new WebServiceException("Unable to get content as string.", exception); } // return content as a string return readContent.toString(); }
From source file:com.frostwire.transfers.BaseHttpDownload.java
static void simpleHTTP(String url, OutputStream out, int timeout) throws Throwable { URL u = new URL(url); URLConnection con = u.openConnection(); con.setConnectTimeout(timeout);/*from ww w . j av a 2 s.c o m*/ con.setReadTimeout(timeout); InputStream in = con.getInputStream(); try { byte[] b = new byte[1024]; int n = 0; while ((n = in.read(b, 0, b.length)) != -1) { out.write(b, 0, n); } } finally { try { out.close(); } catch (Throwable e) { // ignore } try { in.close(); } catch (Throwable e) { // ignore } } }
From source file:org.roda.core.common.SeleniumUtils.java
private static String getHTMLSource(String link) throws IOException { URL url = new URL(link); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String inputLine;/*from www. java2 s .c om*/ StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } IOUtils.closeQuietly(in); return content.toString(); }
From source file:XmlUtils.java
public static Document parse(URL url) throws DocumentException, IOException { URLConnection urlConnection; DataInputStream inStream;/*from ww w . ja v a 2s . c om*/ urlConnection = url.openConnection(); ((HttpURLConnection) urlConnection).setRequestMethod("GET"); urlConnection.setDoInput(true); urlConnection.setDoOutput(false); urlConnection.setUseCaches(false); inStream = new DataInputStream(urlConnection.getInputStream()); byte[] bytes = new byte[1024]; int read; StringBuilder builder = new StringBuilder(); while ((read = inStream.read(bytes)) >= 0) { String readed = new String(bytes, 0, read, "UTF-8"); builder.append(readed); } SAXReader reader = new SAXReader(); XmlUtils.createIgnoreErrorHandler(reader); // InputSource inputSource = new InputSource(new InputStreamReader(inStream, "UTF-8")); // inputSource.setEncoding("UTF-8"); Document dom = reader.read(new StringReader(builder.toString())); inStream.close(); // new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("retrieval.xml"), "UTF-8") return dom; }
From source file:net.grinder.util.NetworkUtil.java
/** * Download a file from the given URL string. * /*from w w w . ja v a 2 s.c o m*/ * @param urlString * URL string * @param toFile * file to be saved. */ public static void downloadFile(String urlString, File toFile) { FileOutputStream os = null; InputStream in = null; URLConnection connection = null; try { URL url = new URL(urlString); connection = url.openConnection(); connection.connect(); byte[] buffer = new byte[4 * 1024]; int read; os = new FileOutputStream(toFile); in = connection.getInputStream(); while ((read = in.read(buffer)) > 0) { os.write(buffer, 0, read); } } catch (Exception e) { LOGGER.error("download file from {} was failed", urlString, e); throw new NGrinderRuntimeException("Error while download " + urlString, e); } finally { ((HttpURLConnection) connection).disconnect(); IOUtils.closeQuietly(os); IOUtils.closeQuietly(in); } return; }
From source file:com.gh4a.utils.ImageDownloader.java
/** * Download bitmap.// ww w.j a va 2s . com * * @param urlStr the url str * @return the bitmap */ static Bitmap downloadBitmap(String urlStr) { InputStream is = null; try { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.connect(); is = conn.getInputStream(); // bis = new BufferedInputStream(is); final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(is)); return bitmap; } catch (Exception e) { Log.e(Constants.LOG_TAG, e.getMessage(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { Log.e(Constants.LOG_TAG, e.getMessage(), e); } } } return null; }
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 www . j a v a 2s . c om*/ 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(); } }