Example usage for java.net URLConnection getInputStream

List of usage examples for java.net URLConnection getInputStream

Introduction

In this page you can find the example usage for java.net URLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.ocp.media.UriTexture.java

private static final BufferedInputStream createInputStreamFromRemoteUrl(String uri,
        ClientConnectionManager connectionManager) {
    InputStream contentInput = null;
    if (connectionManager == null) {
        try {//from  w w  w  .  j av  a  2 s.  co  m
            URL url = new URI(uri).toURL();
            URLConnection conn = url.openConnection();
            conn.connect();
            contentInput = conn.getInputStream();
        } catch (Exception e) {
            Log.w(Gallery.TAG, TAG + ": " + "Request failed: " + uri);
            e.printStackTrace();
            return null;
        }
    } else {
        // We create a cancelable http request from the client
        final DefaultHttpClient mHttpClient = new DefaultHttpClient(connectionManager, HTTP_PARAMS);
        HttpUriRequest request = new HttpGet(uri);
        // Execute the HTTP request.
        HttpResponse httpResponse = null;
        try {
            httpResponse = mHttpClient.execute(request);
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                // Wrap the entity input stream in a GZIP decoder if
                // necessary.
                contentInput = entity.getContent();
            }
        } catch (Exception e) {
            Log.w(Gallery.TAG, TAG + ": " + "Request failed: " + request.getURI());
            return null;
        }
    }
    if (contentInput != null) {
        return new BufferedInputStream(contentInput, 4096);
    } else {
        return null;
    }
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

/**
 * Downloads the given URL to the targetDir and names it
 * {@code targetFilename}. Creates the directory and its parent(s) if it
 * does not exist./* ww w  .  j a  va2 s. c  om*/
 *
 * @param downloadUrl
 * @param targetDir
 * @param targetFilename
 * @throws IOException
 */
public static void download(String downloadUrl, String targetDir, String targetFilename) throws IOException {
    File f = new File(targetDir);
    f.mkdirs();
    URL url = new URL(downloadUrl);
    URLConnection conn = url.openConnection();
    conn.setConnectTimeout(10000);
    InputStream is = conn.getInputStream();
    FileOutputStream out = new FileOutputStream(new File(targetDir, targetFilename));
    IOUtils.copy(is, out);
    close(out);
    close(is);
}

From source file:com.linkedin.pinot.tools.admin.command.AbstractBaseAdminCommand.java

public static String sendPostRequest(String urlString, String payload)
        throws UnsupportedEncodingException, IOException, JSONException {
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();

    conn.setDoOutput(true);/*from   w w w .  ja  v a2  s.c om*/
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

    writer.write(payload, 0, payload.length());
    writer.flush();

    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    final StringBuilder sb = new StringBuilder();
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}

From source file:com.cooliris.media.UriTexture.java

private static final BufferedInputStream createInputStreamFromRemoteUrl(String uri,
        ClientConnectionManager connectionManager) {
    InputStream contentInput = null;
    if (connectionManager == null) {
        try {//  ww w. jav  a  2  s . c o m
            URL url = new URI(uri).toURL();
            URLConnection conn = url.openConnection();
            conn.connect();
            contentInput = conn.getInputStream();
        } catch (Exception e) {
            Log.w(TAG, "Request failed: " + uri);
            e.printStackTrace();
            return null;
        }
    } else {
        // We create a cancelable http request from the client
        final DefaultHttpClient mHttpClient = new DefaultHttpClient(connectionManager, HTTP_PARAMS);
        HttpUriRequest request = new HttpGet(uri);
        // Execute the HTTP request.
        HttpResponse httpResponse = null;
        try {
            httpResponse = mHttpClient.execute(request);
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                // Wrap the entity input stream in a GZIP decoder if
                // necessary.
                contentInput = entity.getContent();
            }
        } catch (Exception e) {
            Log.w(TAG, "Request failed: " + request.getURI());
            return null;
        }
    }
    if (contentInput != null) {
        return new BufferedInputStream(contentInput, 4096);
    } else {
        return null;
    }
}

From source file:co.raveesh.yspages.YSScraper.java

/**
 * Performs the scraping of an individual page from a list on the YourStory Pages website.
 * The information is gathered and an ArrayList of Company objects is returned with appropriate data.
 * @param Url URL of the individual page on the YourStory website
 * @param page The current page number from the list. Required so as to scrape Featured companies from the first page only
 * @return ArrayList<Company> of the companies found on the webpage
 * @throws IOException Thrown if there is an error accessing the webpage
 * @throws XPatherException Thrown if there is a significant error with the evaluateXPath
 *//*from www  . j a  va2 s  .  c o  m*/
public static ArrayList<Company> performScrapingffIndividualPageffList(String Url, int page)
        throws XPatherException, IOException {
    List<Company> companies = new ArrayList<Company>();

    HtmlCleaner cleaner = new HtmlCleaner();
    CleanerProperties props = cleaner.getProperties();
    props.setAllowHtmlInsideAttributes(true);
    props.setAllowMultiWordAttributes(true);
    props.setRecognizeUnicodeChars(true);
    props.setOmitComments(true);

    URL url = new URL(Url);
    URLConnection conn = url.openConnection();
    TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
    if (node.getText().toString().contains("No Companies Found")) {
        System.out.println("Caught 404");
    } else {
        if (page == 1) {
            Object[] companiesFeatured = node.evaluateXPath("//li[@class='companypanel']");
            System.out.println("Getting featured companies information");
            for (int i = 0; i < companiesFeatured.length; i++) {
                companies.add(new Company((TagNode) companiesFeatured[i]));
            }
        }
        Object[] companiesScraped = node.evaluateXPath("//li[@class='companylistitem']");
        System.out.println("Getting company information");
        for (int i = 0; i < companiesScraped.length; i++) {
            companies.add(new Company((TagNode) companiesScraped[i]));
        }
    }
    return (ArrayList<Company>) companies;
}

From source file:com.breadwallet.tools.threads.ImportPrivKeyTask.java

private static String callURL(String myURL) {
    //        System.out.println("Requested URL_EA:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;//  ww w . ja  v  a2 s  .co m
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);

            int cp;
            while ((cp = bufferedReader.read()) != -1) {
                sb.append((char) cp);
            }
            bufferedReader.close();
        }
        assert in != null;
        in.close();
    } catch (Exception e) {
        return null;
    }

    return sb.toString();
}

From source file:com.krawler.esp.utils.HttpPost.java

public static String GetResponse(String postdata) {
    try {/*from   ww  w  .j  a  va  2 s  . c om*/
        String s = URLEncoder.encode(postdata, "UTF-8");
        // URL u = new URL("http://google.com");
        URL u = new URL("http://localhost:7070/service/soap/");
        URLConnection uc = u.openConnection();
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setAllowUserInteraction(false);

        DataOutputStream dstream = new DataOutputStream(uc.getOutputStream());

        // The POST line
        dstream.writeBytes(s);
        dstream.close();

        // Read Response
        InputStream in = uc.getInputStream();
        int x;
        while ((x = in.read()) != -1) {
            System.out.write(x);
        }
        in.close();

        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuffer buf = new StringBuffer();
        String line;
        while ((line = r.readLine()) != null) {
            buf.append(line);
        }
        return buf.toString();
    } catch (Exception e) {
        // throw e;
        return e.toString();
    }
}

From source file:de.anycook.social.facebook.FacebookHandler.java

public static String getPermissions(String accessToken, long facebookID) throws IOException {
    StringBuilder out = new StringBuilder();

    String data = "?access_token=" + accessToken;
    URL url = new URL("https://api.facebook.com/" + facebookID + "/permissions" + data);
    URLConnection conn = url.openConnection();
    //conn.setDoOutput(true);
    //OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    //wr.write(data);
    //wr.flush();

    try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
        String line;//from w w w .  j a v a  2  s  .  c o  m
        while ((line = rd.readLine()) != null) {
            out.append(line);
        }
    }

    return out.toString();
}

From source file:deodex.ui.about.CheckUpdatePan.java

public static void CheckForUpdate(LoggerPan logger) {
    File remoteVersionFile = new File(
            System.getProperty("java.io.tmpdir") + File.separator + "remoteVersion.txt");
    File localeVersionFile = new File(
            System.getProperty("java.io.tmpdir") + File.separator + "locale version.txt");
    URL updateInfo;/*from  w w w. j  a  v  a  2s  .c o  m*/
    try {
        updateInfo = new URL("http://www.lordroid.tk/lordroid/checkupdate.php");
        URLConnection connection = updateInfo.openConnection();
        InputStream is = connection.getInputStream();
        FilesUtils.copyFile(Thread.currentThread().getContextClassLoader().getResourceAsStream("version.txt"),
                localeVersionFile);
        FilesUtils.copyFile(is, remoteVersionFile);
        int localVersion = Integer.parseInt(PropReader.getProp("version.incrimental", localeVersionFile));
        int remoteVersion = Integer.parseInt(PropReader.getProp("version.incrimental", remoteVersionFile));
        if (localVersion < remoteVersion) {
            logger.addLog(R.getString(S.LOG_INFO) + "[" + R.getString("0000094") + "]");
        } else {
            logger.addLog(R.getString(S.LOG_INFO) + "[" + R.getString("0000096") + "]");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.addLog(R.getString(S.LOG_INFO) + "[" + R.getString("0000087") + "]");
        return;
    }

}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
 * Gets the document./*from  w ww  .  ja  v  a  2  s . c  om*/
 * 
 * @param configURL
 *            the config url
 * @return the document
 * @throws Exception
 *             the exception
 */
private static Document getDocument(URL configURL) throws Exception {
    InputStream is = null;
    if (configURL != null) {
        URLConnection conn = configURL.openConnection();
        conn.setUseCaches(false); // avoid JAR locking on Windows and Tomcat
        is = conn.getInputStream();
    }
    if (is == null) {
        throw new IOException("Failed to obtain InputStream from url: " + configURL);
    }

    DocumentBuilderFactory docBuilderFactory = null;
    docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setValidating(true);
    docBuilderFactory.setNamespaceAware(true);

    try {
        // otherwise Xerces fails in validation
        docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true);
    } catch (IllegalArgumentException e) {
        docBuilderFactory.setValidating(false);
        docBuilderFactory.setNamespaceAware(false);
    }

    InputSource source = new InputSource(is);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    // docBuilder.setEntityResolver( resolver );

    List errors = new ArrayList();
    docBuilder.setErrorHandler(new ErrorLogger("XML InputStream", errors));
    Document doc = docBuilder.parse(source);

    if (errors.size() != 0) {
        throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0));
    }
    is.close(); //Close input Stream
    return doc;
}