Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection() throws java.io.IOException 

Source Link

Document

Returns a java.net.URLConnection URLConnection instance that represents a connection to the remote object referred to by the URL .

Usage

From source file:Main.java

public static File downloadFile(String urlstr, File saveFile) {
    try {/*from w  w w . j  a  v  a  2s.co  m*/
        URL url = new URL(urlstr);// cause speed low.
        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.connect();
        InputStream ins = con.getInputStream();
        final int bufsize = 102400;

        byte[] buffer = new byte[bufsize];
        int len = -1;
        FileOutputStream bos = new FileOutputStream(saveFile);
        while ((len = ins.read(buffer)) != -1) {
            bos.write(buffer, 0, len);

        }
        ins.close();
        bos.close();
        return saveFile;
    } catch (Error e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String stringFromHttpGet(String urlString) {
    try {/*from w w w  . ja  v a2  s.c om*/
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
        return null;
    }
}

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);//from   ww  w. j  a  v  a  2 s  .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

public static boolean isAvailable(String urlString) throws IOException {
    try {//from ww w . j  a  v a  2 s.co  m
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        URL url = new URL(urlString);

        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1000);
        urlc.connect();

        if (urlc.getResponseCode() == 200) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * Gets an image from a url and returns it as an inputstream.
 * @param url The absolute url of the image
 * @return an inputstream containing the image
 *///  w w  w .  j av  a2 s. c om
public static InputStream getImageStream(URL stringUrl) {
    InputStream input = null;
    try {
        input = stringUrl.openConnection().getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return input;
}

From source file:Main.java

public static InputStream downloadFromURL(String urlString) {
    InputStream retval = null;/*from  w w w .  ja v a 2  s  .c  o 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

public static void downloadResource(final String fileUrl) {
    new Thread(new Runnable() {

        @Override//from   w ww.  ja  va 2 s. c o m
        public void run() {
            inStream = null;
            isOver = false;
            try {
                URL url = new URL(fileUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                inStream = conn.getInputStream();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            isOver = true;
        }
    }).start();
}

From source file:Main.java

private static String connect(String uri, String charsetName) {
    String result = "";
    try {/* w w w .  j  av a2  s .  c om*/
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        if (Integer.parseInt(Build.VERSION.SDK) < 8) {
            System.setProperty("http.keepAlive", "false");
        }
        if (conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            result = readData(is, charsetName);
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (Exception e) {
    }
    return result;
}

From source file:Main.java

public static String getResponse(String urlParam) {
    try {/*from  w  w w .ja v  a 2 s .  c  o m*/
        URL url = new URL(urlParam);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)");
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = conn.getInputStream();
            return inputStream2String(inputStream);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:Main.java

public static String stringFromHttpPost(String urlStr, String body) {
    HttpURLConnection conn;//  w  w w.java 2 s. c om
    try {
        URL e = new URL(urlStr);
        conn = (HttpURLConnection) e.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestMethod("POST");
        OutputStream os1 = conn.getOutputStream();
        DataOutputStream out1 = new DataOutputStream(os1);
        out1.write(body.getBytes("UTF-8"));
        out1.flush();
        conn.connect();
        String line;
        BufferedReader reader;
        StringBuffer sb = new StringBuffer();
        if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) {
            reader = new BufferedReader(
                    new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"));
        } else {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
    }
    return null;
}