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.groupon.odo.tests.HttpUtils.java

public static String doProxyHttpsGet(String url, BasicNameValuePair[] data) throws Exception {
    String fullUrl = url;/*w  w w . j a va 2 s  .c o m*/

    if (data != null) {
        if (data.length > 0) {
            fullUrl += "?";
        }

        for (BasicNameValuePair bnvp : data) {
            fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&";
        }
    }

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }

    URL uri = new URL(fullUrl);
    int port = Utils.getSystemPort(Constants.SYS_FWD_PORT);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));
    URLConnection connection = uri.openConnection(proxy);

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String accumulator = "";
    String line = "";
    Boolean firstLine = true;
    while ((line = rd.readLine()) != null) {
        accumulator += line;
        if (!firstLine) {
            accumulator += "\n";
        } else {
            firstLine = false;
        }
    }

    return accumulator;
}

From source file:Main.java

private static InputStream openStream(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    if (connection instanceof JarURLConnection) {
        // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014
        connection.setUseCaches(false);/*w  w w  .ja  v a  2 s  . c  o m*/
    }
    InputStream is = connection.getInputStream();
    return is;
}

From source file:net.meltdowntech.steamstats.SteamUser.java

public static List<SteamUser> fetchUsers(List<Long> ids) {
    List<SteamUser> users = new ArrayList<>();
    int queries = (int) Math.ceil(ids.size() / 100.0);

    //Query every hundred users
    Util.printDebug("Fetching users");
    Util.printInfo(String.format("%d user%s allotted", ids.size(), ids.size() == 1 ? "" : "s"));
    Util.printDebug(String.format("%d quer%s allotted", queries, queries == 1 ? "y" : "ies"));
    for (int x = 0; x < queries; x++) {
        //Create steam ids query string
        String steamIds = "&steamids=";
        for (int y = x * 100; y < (x + 1) * 100 && y < ids.size(); y++) {
            steamIds += ids.get(y) + ",";
        }/*from  w w  w .jav  a 2  s.com*/

        //Create query url
        String url = Steam.URL + Steam.PLAYER_DATA + "?key=" + Steam.KEY + steamIds;
        try {
            //Attempt connection and parse
            URL steam = new URL(url);
            URLConnection steamConn = steam.openConnection();
            Reader steamReader = new InputStreamReader(steamConn.getInputStream());
            JSONTokener steamTokener = new JSONTokener(steamReader);
            JSONObject data = (JSONObject) steamTokener.nextValue();
            JSONObject response = data.getJSONObject("response");
            JSONArray players = response.getJSONArray("players");

            //Parse each player
            for (int y = 0; y < players.length(); y++) {
                JSONObject player = players.getJSONObject(y);
                SteamUser user = new SteamUser();

                //Parse each field
                Iterator keys = player.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    user.values.put(key, player.get(key));
                }
                users.add(user);
            }
        } catch (MalformedURLException ex) {
            Util.printError("Invalid URL");
        } catch (IOException | JSONException ex) {
            Util.printError("Invalid data");
        } catch (Exception ex) {
            Util.printError("Generic error");
        }

    }
    Util.printDebug("Done fetching users");
    return users;
}

From source file:Main.java

private static CharSequence consume(URLConnection connection, int maxChars) throws IOException {
    String encoding = getEncoding(connection);
    StringBuilder out = new StringBuilder();
    Reader in = null;//from w  ww. j a  v  a2s.  c  o  m
    try {
        in = new InputStreamReader(connection.getInputStream(), encoding);
        char[] buffer = new char[1024];
        int charsRead;
        while (out.length() < maxChars && (charsRead = in.read(buffer)) > 0) {
            out.append(buffer, 0, charsRead);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
                // continue
            } catch (NullPointerException ioe) {
                // continue
            }
        }
    }
    return out;
}

From source file:com.netspective.sparx.util.HttpUtils.java

public static void includeUrlContent(String spec, Writer writer) throws IOException {
    URL url = new URL(spec);
    URLConnection urlConn = url.openConnection();
    InputStream urlIn = urlConn.getInputStream();
    int iRead = urlIn.read();
    while (iRead != -1) {
        writer.write((char) iRead);
        iRead = urlIn.read();//ww w.j a va 2  s  .  c o m
    }
}

From source file:GetItemList.java

/**
 * Retrieve the contents of a URL/*from   w  ww .  ja v a2 s . c  om*/
 *
 */
public static String getPage(String path) throws Exception {
    // Create the URL from the given path
    URL url = new URL(path);

    // We won't bother checking that everything's okay
    URLConnection url_conn = url.openConnection();

    // Create a reader for the input stream
    BufferedReader read = new BufferedReader(new InputStreamReader(url_conn.getInputStream()));

    // read through each line and separate with the EOL string
    String out = "";
    String line = "";
    while ((line = read.readLine()) != null) {
        out += line + EOL_URL;
    }

    // Be sure to tidy up
    read.close();

    // Return the contents of the URL
    return out;
}

From source file:com.ibm.jaql.lang.expr.hadoop.Util.java

/**
 * Modified from Hadoop's JobClient/*from  ww w  .  j  a  v  a2 s .  c o  m*/
 * 
 * @param taskId
 * @param taskLogUrl
 */
private static void logTaskLog(TaskAttemptID taskId, URL taskLogUrl) {
    try {
        URLConnection connection = taskLogUrl.openConnection();
        BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        try {
            String logData = null;
            while ((logData = input.readLine()) != null) {
                if (logData.length() > 0) {
                    LOG.info("[" + taskId + "]" + logData);
                }
            }
        } finally {
            input.close();
        }
    } catch (IOException ioe) {
        LOG.warn("Error reading task output" + ioe.getMessage());
    }
}

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

public static String getUsersOAuthToken(long facebook_id) throws IOException {
    URL url = new URL("https://graph.facebook.com/anycook.oauth/access_token?" + "client_id=" + APP_ID + "&"
            + "client_secret=" + APP_SECRET + "&" + "grant_type=client_credentials");
    URLConnection urlconnection = url.openConnection();
    BufferedReader rd = new BufferedReader(new InputStreamReader(urlconnection.getInputStream()));
    String response = rd.readLine();
    return response.split("=")[1];

}

From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java

/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path./*from w  w  w . j  av  a  2s.  c  om*/
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
    Assert.notNull(resourceName, "Resource name must not be null");
    ClassLoader classLoaderToUse = classLoader;
    if (classLoaderToUse == null) {
        classLoaderToUse = ClassUtils.getDefaultClassLoader();
    }
    Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName)
            : ClassLoader.getSystemResources(resourceName));
    Properties props = new Properties();
    while (urls.hasMoreElements()) {
        URL url = urls.nextElement();
        URLConnection con = url.openConnection();
        ResourceUtils.useCachesIfNecessary(con);
        InputStream is = con.getInputStream();
        try {
            if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
                props.loadFromXML(is);
            } else {
                props.load(is);
            }
        } finally {
            is.close();
        }
    }
    return props;
}

From source file:org.apache.ambari.view.weather.CityResourceProvider.java

private static InputStream readFrom(String spec) throws IOException {
    URLConnection connection = new URL(spec).openConnection();

    connection.setConnectTimeout(5000);//from w  w  w .  ja  v  a2s.  c  o m
    connection.setDoOutput(true);
    return connection.getInputStream();
}