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 String httpGet(String urlStr, List<NameValuePair> params) {

    String paramsEncoded = "";
    if (params != null) {
        paramsEncoded = URLEncodedUtils.format(params, "UTF-8");
    }//from  www  .  j ava2s  .  com

    String fullUrl = urlStr + "?" + paramsEncoded;

    String result = null;
    URL url = null;
    HttpURLConnection connection = null;
    InputStreamReader in = null;
    try {
        url = new URL(fullUrl);
        connection = (HttpURLConnection) url.openConnection();
        in = new InputStreamReader(connection.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(in);
        StringBuffer strBuffer = new StringBuffer();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            strBuffer.append(line);
        }
        result = strBuffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    return result;
}

From source file:com.compomics.colims.core.util.AccessionConverter.java

/**
 * convenience method for opening a buffered reader to an url, standard
 * timeout is set to 500 milliseconds/*from w  ww.jav  a 2  s .  c  om*/
 *
 * @param aUrl the url to open a reader to
 * @return the buffered reader for reading the url
 * @throws IOException if the url could not be read
 */
public static BufferedReader openReader(String aUrl) throws IOException {
    URL myURL = new URL(aUrl);
    HttpURLConnection c = (HttpURLConnection) myURL.openConnection();
    c.setConnectTimeout(500);
    return new BufferedReader(new InputStreamReader(new BufferedInputStream(c.getInputStream()), "UTF-8"));
}

From source file:com.example.admin.processingboilerplate.JsonIO.java

public static StringBuilder load(HttpURLConnection con) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;/*  www .  j  a  v  a2  s  .  com*/
    try {
        br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        String s;
        while ((s = br.readLine()) != null)
            sb.append(s).append('\n');
    } catch (IOException e) {
        Log.e("loadJson", e.getMessage(), e);
    } finally {
        try {
            if (br != null)
                br.close();
            con.disconnect();
        } catch (IOException e) {
            Log.e("loadJson", e.getMessage(), e);
        }
    }
    return sb;
}

From source file:com.exzogeni.dk.http.HttpTask.java

@NonNull
private static InputStream getInputStream(HttpURLConnection cn) {
    try {/*www  . j ava2s  .c o m*/
        return cn.getInputStream();
    } catch (IOException e) {
        final InputStream stream = cn.getErrorStream();
        if (stream == null) {
            return new ByteArrayInputStream(new byte[0]);
        }
        return stream;
    }
}

From source file:com.telefonica.iot.perseo.test.Help.java

private static String getBodyResponse(HttpURLConnection con) throws IOException {
    int responseCode = con.getResponseCode();
    InputStream stream;//from w  w w  .j av  a 2 s  .  c o m
    if (responseCode / 100 == 2) {
        stream = con.getInputStream();
    } else {
        stream = con.getErrorStream();
    }
    if (stream != null) {
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }
    return null;
}

From source file:Main.java

public static Document getUrlAsDocument(String urlAsString, int timeout) throws Exception {
    URL url = new URL(urlAsString);
    //using proxy may increase latency
    HttpURLConnection hConn = (HttpURLConnection) url.openConnection();
    hConn.setReadTimeout(timeout);/*from  w w w.jav  a 2  s  . c o  m*/
    hConn.setConnectTimeout(timeout);
    //        hConn.setRequestProperty("Accept-Encoding", "gzip, deflate");

    InputStream is = hConn.getInputStream();
    //        if ("gzip".equals(hConn.getContentEncoding()))
    //            is = new GZIPInputStream(is);
    return newDocumentBuilder().parse(is);
}

From source file:com.wabbit.libraries.remoting.HttpHelper.java

static public InputStream loadImage(String url) {
    HttpURLConnection connection = null;
    InputStream is = null;//from w  w w .  ja va 2s .c o  m

    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(20000);

        is = new BufferedInputStream(connection.getInputStream());
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return is;
}

From source file:com.dlshouwen.core.api.controller.ApiController.java

/**
 * base64/*from   w  w  w . j  a v a  2s . c o  m*/
 * @param path
 * @return 
 */
public static String GetImageStr(String path) {//Base64??
    InputStream in = null;
    byte[] data = null;
    //?
    try {
        URL url = new URL(path);
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();
        in = httpUrl.getInputStream();
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Base64?
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(data);//Base64?
}

From source file:Main.java

public static InputStream getHTTPInputStream(String url) {
    try {//from  w  w  w .j a v  a 2s.com
        URL urlObj = null;
        urlObj = new URL(url);

        HttpURLConnection urlConnection = null;

        urlConnection = (HttpURLConnection) urlObj.openConnection();

        urlConnection.connect();

        int response = urlConnection.getResponseCode();

        if (response != 200) {
            return null;
        }

        return urlConnection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static InputStream downloadURL(String link) throws IOException {
    URL url = new URL(link);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);// ww  w .j  a  v  a  2 s. co  m
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    logInfo("downloadStatus: " + conn.getResponseCode());
    return conn.getInputStream();
}