Example usage for java.net HttpURLConnection getInputStream

List of usage examples for java.net HttpURLConnection getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:Main.java

public static byte[] getBytes(String url) {
    try {//from   www. jav  a2  s. c o  m
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Charset", "UTF-8");
        InputStream is = connection.getInputStream();
        int contentLength = connection.getContentLength();
        byte[] data = new byte[contentLength];
        int alreadyRead = 0;
        int len = 0;
        while (alreadyRead < contentLength) {
            len = is.read(data, alreadyRead, contentLength - alreadyRead);
            alreadyRead += len;
        }
        is.close();
        return data;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static Bitmap getImage(String urlpath) throws Exception {
    Bitmap bitmap = null;/* w ww  . j  av  a  2  s.co m*/
    URL url = new URL(urlpath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);
    if (conn.getResponseCode() == 200) {
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
}

From source file:be.roots.taconic.pricingguide.util.HttpUtil.java

public static String readString(String urlAsString, String userName, String password) {

    try {//ww w . j av a2  s .  com
        final HttpURLConnection con = getInputStreamFor(urlAsString, userName, password);
        final BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        final String response = IOUtils.toString(in);
        IOUtils.closeQuietly(in);
        return response;
    } catch (IOException e) {
        LOGGER.error(e.getLocalizedMessage(), e);
    }
    return null;

}

From source file:com.company.project.core.connector.HttpClientHelper.java

public static byte[] getByteaArrayFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

    InputStream is = conn.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int bytesRead = 0;

    while ((bytesRead = is.read(buff, 0, buff.length)) != -1) {
        baos.write(buff, 0, bytesRead);/*from  w w  w.  j ava2 s .  c o m*/
    }

    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}

From source file:me.bramhaag.discordselfbot.util.Util.java

/**
 * Get image from URL./*from www.j  a  v  a  2 s.  c  o m*/
 * @param url url to get image from.
 * @return image.
 *
 * @throws IOException if an I/O error occurs while creating the input stream.
 */
@NonNull
public static BufferedImage getImage(@NonNull String url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-Agent", Constants.USER_AGENT);

    return ImageIO.read(connection.getInputStream());
}

From source file:Main.java

public static InputStream downloadFromURL(String urlString) {
    InputStream retval = null;//from  ww w  . j a  v a2  s.  co  m
    try {
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //URLConnection ucon = url.openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        retval = con.getInputStream();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retval;
}

From source file:Main.java

private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException {
    InputStream inputStream = null;

    try {//from  w ww.  j a va2  s .c  o m
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();
        conn.setUseCaches(cache);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
            inputStream = httpConn.getInputStream();

    } catch (Exception ex) {
    }
    return inputStream;
}

From source file:Clima.Clima.java

public static String getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;// w w w . ja  v a 2s. c o  m
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}

From source file:Main.java

public static byte[] getHtmlByteArray(final String url) {
    URL htmlUrl;// ww w.  j av  a  2 s  . co m
    InputStream inStream = null;
    try {
        htmlUrl = new URL(url);
        URLConnection connection = htmlUrl.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inStream = httpConnection.getInputStream();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] data = inputStreamToByte(inStream);
    return data;
}

From source file:Main.java

public static Bitmap decodeBitmapFromURL(String src, boolean large) {

    // If the artwork returned null, don't want to try to show artwork
    if (src.equals("null")) {
        return null;
    }//w w  w . j a  v a 2  s.  c om

    if (large) {
        src = src.replace("large", "t500x500");
    }

    InputStream inputStream = null;
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        inputStream = connection.getInputStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (inputStream == null) {
        return null;
    }

    // Decided not to scale because would have to recreate input stream
    /*
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, options);
            
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
            
    options.inJustDecodeBounds = false; */
    Bitmap returnBitmap = BitmapFactory.decodeStream(inputStream);
    return returnBitmap;
}