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 boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {// w  w w.  j ava 2s.  c  om
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:com.adguard.compiler.UrlUtils.java

public static String downloadString(URL url, String encoding, String userAgent) throws IOException {

    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {//from w  w  w. j a  v a  2s .c  o m
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", userAgent);
        connection.connect();
        inputStream = connection.getInputStream();
        return IOUtils.toString(inputStream, encoding);
    } finally {
        IOUtils.closeQuietly(inputStream);
        if (connection != null) {
            connection.disconnect();
        }
    }
}

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

public static String getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

    BufferedReader reader = null;
    if (isSuccess) {
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } else {/*from   w  w w  .j a v a 2  s. c o m*/
        reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    }
    StringBuffer stringBuffer = new StringBuffer();
    String line = "";
    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line);
    }

    return stringBuffer.toString();
}

From source file:com.tc.util.io.ServerURL.java

private static String readLines(HttpURLConnection urlConnection) throws IOException {
    try {/*w  ww .  j a  v  a2s  . c o  m*/
        List<String> lines = IOUtils.readLines(urlConnection.getInputStream());
        StringBuilder sb = new StringBuilder();
        for (String line : lines) {
            sb.append(line).append(System.getProperty("line.separator"));
        }
        return sb.toString();
    } catch (IOException e) {
        return "";
    }
}

From source file:Main.java

public static String requestData(String address) throws Exception {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);/*from   w ww.  ja  v  a  2 s  .c  o  m*/
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}

From source file:Main.java

public static String requestData(String address) throws IOException {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);/* ww  w . j av  a  2s .  c om*/
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}

From source file:Main.java

/**
 * Downloads a file via HTTP(S) GET to the given path. This function cannot be called from the
 * UI thread. Android does not allow it.
 *
 * @param urlString Url to the ressource to download.
 * @param file      file to be written to.
 * @param overwrite if file exists, overwrite?
 * @return flase if download was not successful. If successful, true.
 *//*from w w  w.  j  a  v  a 2s. com*/
private static Boolean fileDownloadHttp(String urlString, File file, Boolean overwrite) {
    HashMap<String, String> result = null;
    URL url = null;
    //        File temp;

    try {
        url = new URL(urlString);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(200000);
        urlConnection.connect();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        FileOutputStream outputStream = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        in.close();
        outputStream.close();
        urlConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    Log.d(TAG, "File download: " + file.getAbsolutePath() + url.getFile() + "overwrite " + overwrite
            + "exists? " + file.exists());
    return true;
}

From source file:Main.java

public static String getJsonWithPath(String path) {
    StringBuffer sb = new StringBuffer();
    URL url = null;/*from w  w w .j a v  a 2s .com*/
    HttpURLConnection conn = null;
    BufferedReader br = null;

    try {
        url = new URL(path);
        conn = (HttpURLConnection) url.openConnection();
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String temp = "";
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
        } else {
            Log.e(TAG, " NET IS ERROR");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(br);
        conn.disconnect();
    }
    Log.i("TAG", "---" + sb.toString());
    return sb.toString();
}

From source file:Main.java

static public JSONArray loadJSON(String url) {
    HttpURLConnection connection = null;
    JSONArray json = null;//  w  ww .ja v  a2 s  .  c om
    InputStream is = null;

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

        is = new BufferedInputStream(connection.getInputStream());
        json = new JSONArray(convertStreamToString(is));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (JSONException je) {
        je.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (connection != null) {
            connection.disconnect();
        }
    }

    return json;
}

From source file:Main.java

public static String getServerResponse(String urlRequest) {

    Log.d("urlRequest", urlRequest);
    String response = "";
    HttpURLConnection conn = null;
    try {//from  www.  j a v  a  2  s .c  o  m
        conn = (HttpURLConnection) new URL(urlRequest).openConnection();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        response = read(conn.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d("response", response);
    return response.trim();
}