List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:mc.lib.network.NetworkHelper.java
public static void saveToFile(String url, File file) { InputStream is = null;// w w w . j a v a 2 s.c o m OutputStream os = null; try { URLConnection c = new URL(url).openConnection(); c.connect(); is = c.getInputStream(); os = new FileOutputStream(file); StreamHelper.copyNetworkStream(is, os, c.getReadTimeout()); } catch (MalformedURLException e) { Log.e(LOGTAG, "Wrong url format", e); } catch (Exception e) { Log.e(LOGTAG, "Can not open stream", e); } finally { StreamHelper.close(os); StreamHelper.close(is); } }
From source file:mc.lib.network.NetworkHelper.java
public static String getAsText(String url, String encoding) { InputStream is = null;//w ww . ja v a2s. c o m try { URLConnection c = new URL(url).openConnection(); c.connect(); is = c.getInputStream(); if (c.getContentEncoding() != null) encoding = c.getContentEncoding(); return StreamHelper.readNetworkStream(is, encoding, c.getReadTimeout()); } catch (MalformedURLException e) { Log.e(LOGTAG, "Wrong url format", e); } catch (Exception e) { Log.e(LOGTAG, "Cannot get text", e); } finally { StreamHelper.close(is); } return null; }
From source file:biz.wolschon.finance.jgnucash.helper.HttpFetcher.java
/** * Fetch the given URL with http-basic-auth. * @param url the URL/*w w w.j a v a 2s . c om*/ * @param username username * @param password password * @return the page-content or null */ public static InputStream fetchURLStream(final URL url, final String username, final String password) { try { String userPassword = username + ":" + password; // Encode String String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); URLConnection uc = url.openConnection(); uc.setRequestProperty("Authorization", "Basic " + encoding); return uc.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); LOGGER.error("cannot fetch malformed URL " + url, e); return null; } catch (IOException e) { e.printStackTrace(); LOGGER.error("cannot fetch URL " + url, e); return null; } }
From source file:Main.java
public static InputStream getInputEncoding(URLConnection connection) throws IOException { InputStream in;//from ww w .ja va2 s . c om String encoding = connection.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("gzip")) { in = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { in = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { in = connection.getInputStream(); } return in; }
From source file:com.hack23.cia.service.external.common.impl.XmlAgentImpl.java
/** * Read input stream./*from w w w .ja v a 2s. co m*/ * * @param accessUrl * the access url * @return the string * @throws Exception * the exception */ private static String readInputStream(final String accessUrl) throws Exception { final URL url = new URL(accessUrl.replace(" ", "")); final URLConnection connection = url.openConnection(); final InputStream stream = connection.getInputStream(); final BufferedReader inputStream = new BufferedReader(new InputStreamReader(stream)); return readWithStringBuffer(inputStream); }
From source file:com.vaadin.tools.ReportUsage.java
private static void doHttpGet(String userAgent, String url) { Throwable caught;//w w w . j a va2 s .co m InputStream is = null; try { URL urlToGet = new URL(url); URLConnection conn = urlToGet.openConnection(); conn.setRequestProperty(USER_AGENT, userAgent); is = conn.getInputStream(); // TODO use the results IOUtils.toByteArray(is); return; } catch (MalformedURLException e) { caught = e; } catch (IOException e) { caught = e; } finally { IOUtils.closeQuietly(is); } Logger.getLogger(ReportUsage.class.getName()) .fine("Caught an exception while executing HTTP query: " + caught.getMessage()); }
From source file:com.vish.phishDetect.Web_Utilities.java
/** * @param _url/*from ww w .ja v a 2s . c o m*/ * @return google page rank of given url if it exists else returns -10 */ public static int getPageRank(String _url) { String domain = _url; String result = ""; JenkinsHash jenkinsHash = new JenkinsHash(); long hash = jenkinsHash.hash(("info:" + domain).getBytes()); //Append a 6 in front of the hashing value. String url = "http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&" + "ch=6" + hash + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + domain; //System.out.println("Sending request to : " + url); try { URLConnection conn = new URL(url).openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String input; while ((input = br.readLine()) != null) { // What Google returned? Example : Rank_1:1:9, PR = 9 //System.out.println(input); result = input.substring(input.lastIndexOf(":") + 1); } } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println("pr " + result); if ("".equals(result)) { return -5; } else { return Integer.valueOf(result); } }
From source file:$.FileUtils.java
public static void urlToPath(URL url, File fOut) throws IOException { URLConnection uc = url.openConnection(); logger.info("ContentType: " + uc.getContentType()); InputStream in = uc.getInputStream(); org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut); logger.info("File of length " + fOut.length() + " created from URL " + url.toString()); in.close();//from ww w .j a v a 2 s . c o m }
From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java
public static InputStream getInputStream(String fileOrUrl, boolean unzip) { InputStream in = null;// w w w . j a va 2 s.co m try { if (fileOrUrl.matches("https?://.*")) { URL u = new URL(fileOrUrl); URLConnection uc = u.openConnection(); in = uc.getInputStream(); } else { // 1) check if file is within jar in = IOUtils.class.getClassLoader().getResourceAsStream(fileOrUrl); // windows File.separator is \, but getting resources only works with / if (in == null) { in = IOUtils.class.getClassLoader().getResourceAsStream(fileOrUrl.replaceAll("\\\\", "/")); } // 2) if not found in jar, load from the file system if (in == null) { in = new FileInputStream(fileOrUrl); } } // unzip if necessary if ((unzip) && (fileOrUrl.endsWith(".gz"))) { in = new GZIPInputStream(in, GZIP_FILE_BUFFER_SIZE); } // buffer input stream in = new BufferedInputStream(in); } catch (FileNotFoundException e) { LOG.error("FileNotFoundException: " + e.getMessage()); } catch (IOException e) { LOG.error("IOException: " + e.getMessage()); } return in; }
From source file:ota.otaupdates.utils.Utils.java
public static void DownloadFromUrl(String download_url, String fileName) { final String TAG = "Downloader"; if (!isMainThread()) { try {//from w w w .j ava 2 s. co m URL url = new URL(download_url); if (!new File(DL_PATH).isDirectory()) { if (!new File(DL_PATH).mkdirs()) { Log.e(TAG, "Creating the directory " + DL_PATH + "failed"); } } File file = new File(DL_PATH + fileName); long startTine = System.currentTimeMillis(); Log.d(TAG, "Beginning download of " + url.getPath() + " to " + DL_PATH + fileName); /* * Open a connection and define Streams */ URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* * Read bytes until there is nothing left */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert Bytes to a String */ FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); Log.d(TAG, "Download finished in " + ((System.currentTimeMillis() - startTine) / 1000) + " seconds"); } catch (Exception e) { Log.e(TAG, "Error: " + e); e.printStackTrace(); } } else { Log.e(TAG, "Tried to run in Main Thread. Aborting..."); } }