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:mas.MAS_TOP_PAPERS.java

/**
 * @param args the command line arguments
 *///  w w w.  j a v  a2  s. co  m
public static String getData_old(String url_org, int start) {
    try {
        String complete_url = url_org + "&$skip=" + start;
        //            String url_str = generateURL(url_org, prop);
        URL url = new URL(complete_url);
        URLConnection yc = url.openConnection();
        yc.setConnectTimeout(25 * 1000);
        yc.setReadTimeout(25 * 1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        StringBuffer result = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            //                System.out.println(inputLine);
            result.append(inputLine);
        }
        in.close();
        return result.toString();
    } catch (MalformedURLException ex) {
        Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

/**
 * Post contents of input stream to URL.
 * //from  w  w w  .j a v a2  s  .  c  om
 * @param url
 * @param stream
 * @return
 * @throws IOException
 */
protected static String postBase64(URL url, InputStream stream) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    byte[] buff = streamToByteArray(stream);

    String em = Base64.encodeBytes(buff);
    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(em);
    osr.flush();

    stream.close();
    conn.getOutputStream().flush();
    conn.getOutputStream().close();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    rd.close();

    return resp;
}

From source file:com.hazelcast.stabilizer.Utils.java

public static String getText(String url) throws IOException {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();

    BufferedReader in = null;//from www.  ja  v a 2 s.co  m
    try {
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            response.append(inputLine);

        in.close();

        return response.toString();
    } finally {
        closeQuietly(in);
    }
}

From source file:mas.MAS_VLDB.java

/**
 * @param args the command line arguments
 *//*from  w ww.ja  v a 2 s  . c o m*/
public static String getData_old(String url_org, int start) {
    try {
        String complete_url = url_org + "&$skip=" + start;
        //            String url_str = generateURL(url_org, prop);
        URL url = new URL(complete_url);
        URLConnection yc = url.openConnection();
        yc.setConnectTimeout(25 * 1000);
        yc.setReadTimeout(25 * 1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        StringBuffer result = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            //                System.out.println(inputLine);
            result.append(inputLine);
        }
        in.close();
        return result.toString();
    } catch (MalformedURLException ex) {
        Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MAS_VLDB.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.t3.persistence.FileUtil.java

/**
 * Given a URL this method determines the content type of the URL (if possible) and
 * then returns a Reader with the appropriate character encoding.
 * //from   w w  w . java 2 s.  com
 * @param url the source of the data stream
 * @return String representing the data
 * @throws IOException
 */
public static InputStream getURLAsInputStream(URL url) throws IOException {
    InputStream is = null;
    URLConnection conn = null;
    // We're assuming character here, but it could be bytes.  Perhaps we should
    // check the MIME type returned by the network server?
    conn = url.openConnection();
    if (log.isDebugEnabled()) {
        String type = URLConnection.guessContentTypeFromName(url.getPath());
        log.debug("result from guessContentTypeFromName(" + url.getPath() + ") is " + type);
        type = getContentType(conn.getInputStream());
        log.debug("result from getContentType(" + url.getPath() + ") is " + type);
    }
    is = conn.getInputStream();
    return is;
}

From source file:net.sf.jabref.importer.fetcher.ACMPortalFetcher.java

private static Optional<BibEntry> downloadEntryBibTeX(String id, boolean downloadAbstract) {
    try {//from w  w  w  .  ja  va 2s  .co m
        URL url = new URL(ACMPortalFetcher.START_URL + ACMPortalFetcher.BIBTEX_URL + id
                + ACMPortalFetcher.BIBTEX_URL_END);
        URLConnection connection = url.openConnection();

        // set user-agent to avoid being blocked as a crawler
        connection.addRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
        Collection<BibEntry> items = null;
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            items = BibtexParser.parse(in).getDatabase().getEntries();
        } catch (IOException e) {
            LOGGER.info("Download of BibTeX information from ACM Portal failed.", e);
        }
        if ((items == null) || items.isEmpty()) {
            return Optional.empty();
        }
        BibEntry entry = items.iterator().next();
        Thread.sleep(ACMPortalFetcher.WAIT_TIME);//wait between requests or you will be blocked by ACM

        // get abstract
        if (downloadAbstract) {
            URLDownload dl = new URLDownload(ACMPortalFetcher.START_URL + ACMPortalFetcher.ABSTRACT_URL + id);
            String page = dl.downloadToString(Globals.prefs.getDefaultEncoding());

            Matcher absM = ACMPortalFetcher.ABSTRACT_PATTERN.matcher(page);
            if (absM.find()) {
                entry.setField(FieldName.ABSTRACT, absM.group(1).trim());
            }
            Thread.sleep(ACMPortalFetcher.WAIT_TIME);//wait between requests or you will be blocked by ACM
        }

        return Optional.of(entry);
    } catch (NoSuchElementException e) {
        LOGGER.info("Bad BibTeX record read at: " + ACMPortalFetcher.BIBTEX_URL + id
                + ACMPortalFetcher.BIBTEX_URL_END, e);
    } catch (MalformedURLException e) {
        LOGGER.info("Malformed URL.", e);
    } catch (IOException e) {
        LOGGER.info("Cannot connect.", e);
    } catch (InterruptedException ignored) {
        // Ignored
    }
    return Optional.empty();
}

From source file:PostTest.java

/**
 * Makes a POST request and returns the server response.
 * @param urlString the URL to post to/*ww  w.  j a v a 2  s  .  co  m*/
 * @param nameValuePairs a map of name/value pairs to supply in the request.
 * @return the server reply (either from the input stream or the error stream)
 */
public static String doPost(String urlString, Map<String, String> nameValuePairs) throws IOException {
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());

    boolean first = true;
    for (Map.Entry<String, String> pair : nameValuePairs.entrySet()) {
        if (first)
            first = false;
        else
            out.print('&');
        String name = pair.getKey();
        String value = pair.getValue();
        out.print(name);
        out.print('=');
        out.print(URLEncoder.encode(value, "UTF-8"));
    }

    out.close();

    Scanner in;
    StringBuilder response = new StringBuilder();
    try {
        in = new Scanner(connection.getInputStream());
    } catch (IOException e) {
        if (!(connection instanceof HttpURLConnection))
            throw e;
        InputStream err = ((HttpURLConnection) connection).getErrorStream();
        if (err == null)
            throw e;
        in = new Scanner(err);
    }

    while (in.hasNextLine()) {
        response.append(in.nextLine());
        response.append("\n");
    }

    in.close();
    return response.toString();
}

From source file:com.snaplogic.snaps.checkfree.SoapExecuteTest.java

static String getContentFrom(URL fileUrl) {
    String templateText;/*w ww.ja v  a 2s.com*/
    try {
        URLConnection urlConnection = fileUrl.openConnection();
        if (urlConnection == null) {
            throw new ExecutionException(ERR_URL_CONNECT).formatWith(fileUrl.toString())
                    .withReason(REASON_URL_CONNECT).withResolution(RESOLUTION_URL_CONNECT);
        }
        urlConnection.connect();
        try (final InputStream in = urlConnection.getInputStream()) {
            templateText = IOUtils.toString(in);
            if (StringUtils.isBlank(templateText)) {
                throw new ExecutionException(TEMPLATE_READ_FAIL).formatWith(fileUrl.toString())
                        .withReason(EMPTY_TEMPLATE).withResolution(CHECK_TEMPLATE_CONTENTS);
            }
        }
    } catch (IOException e) {
        throw new ExecutionException(TEMPLATE_READ_FAIL).formatWith(fileUrl.toString())
                .withReason(ERROR_READING_TEMPLATE).withResolution(CHECK_TEMPLATE_CONTENTS);
    }
    return templateText;
}

From source file:at.ac.uniklu.mobile.sportal.util.Utils.java

/**
 * Downloads a file from the network./*  w w w  .ja  va2s  .  com*/
 * 
 * This function can fail in some cases (e.g. slow network), so if you really need the file, it
 * is recommended to do a few retries.
 * source: http://code.google.com/p/android/issues/detail?id=6066
 *
 * @param sourceUrl the url where the file is located
 * @param retries the number of retries in case the download fails
 * @return the stream if successful, else null
 */
public static byte[] downloadFile(String sourceUrl, int retries) {
    byte[] data = null;

    boolean successful = false;
    retries++; // add the first try
    do {
        try {
            retries--;
            URL url = new URL(sourceUrl);
            URLConnection urlConnection = url.openConnection();

            // set session cookie for authorization purposes (loading id pictures without being logged in is now [2011-07-04] disabled)
            Cookie cookie = Studentportal.getSportalClient().getSessionCookie();
            if (cookie != null) {
                urlConnection.addRequestProperty("Cookie", cookieHeaderString(cookie));
            }

            InputStream in = new BufferedInputStream(urlConnection.getInputStream(), 1024 * 32);
            data = streamToArray(in);
            in.close();
            successful = true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // if the file doesn't exist, it's pointless to retry
            Log.d(TAG, "file does not exist: " + sourceUrl);
            retries = 0;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } while (!successful && retries > 0);

    return data;
}

From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java

@SuppressWarnings("restriction")
public static String readStringFromUrl(String url, String username, String password) throws IOException {

    URLConnection connection = new URL(url).openConnection();
    connection.setConnectTimeout(5000);//  w  ww .  ja  v a  2 s .co m
    connection.setReadTimeout(5000);
    connection.setDoOutput(true);

    if (username != null) {
        String userpass = username + ":" + password;
        // TODO: Use apache commons instead.
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
        connection.setRequestProperty("Authorization", basicAuth);
    }
    InputStream in = connection.getInputStream();

    try {
        return IOUtils.toString(in, "UTF-8");
    } finally {
        in.close();
    }
}