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 boolean download(String uri, String filePath) {
    try {/*from  w w  w. j  a v  a  2s  . c o m*/
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", AGENT);
        readStreamToFile(conn.getInputStream(), filePath);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static Bitmap getBitmapFromUrl(String urlSource) {
    try {/* ww  w  .j a v  a  2s. c  o m*/
        URL url = new URL(urlSource);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] downloadImage(String imageUrl) {
    if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".bmp") || imageUrl.endsWith(".png")
            || imageUrl.endsWith(".gif")) {
        try {//  w  w w.  jav  a  2s .  c  om
            URL url = new URL(imageUrl);
            URLConnection urlConn = url.openConnection();
            InputStream is = urlConn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:com.jjtree.utilities.JServeletManager.java

public static JSONObject fetchFrom(HttpServletRequest request, String url) {
    JSONObject object = null;/*from   ww  w. j  a v  a2 s.  c  o m*/
    try {
        String serverName = request.getServerName();
        int portNumber = request.getServerPort();
        String contextPath = request.getContextPath();

        String accountUrl = "http://" + serverName + ":" + portNumber + contextPath + url;

        URL urldemo = new URL(accountUrl);
        URLConnection urlCon = urldemo.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            object = new JSONObject(inputLine);
        }
        in.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return object;
}

From source file:Main.java

public static String execUrl(String uri) {
    String res = "";
    try {//  ww w .jav  a  2 s .c  om
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", AGENT);

        res = readStream(conn.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        res = "";
    }
    return res;
}

From source file:com.digitallizard.bbcnewsreader.resource.web.HtmlParser.java

/**
 * @param args//ww  w .j  av a  2  s.c o m
 * @throws IOException
 * @throws ClientProtocolException
 */
public static byte[] getPage(String stringUrl) throws Exception {
    URL url = new URL(stringUrl);
    URLConnection connection = url.openConnection();

    InputStream stream = connection.getInputStream();
    BufferedInputStream inputbuffer = new BufferedInputStream(stream);

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }
    return arraybuffer.toByteArray();
}

From source file:Main.java

public static String requestPage(String urlPath) throws Exception {
    URL url = new URL(urlPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
        throw new Exception("Error loading page: " + responseCode + connection.getResponseMessage());
    }//from www  .  j  av a2 s  .  c  o m

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    String response = "";

    while ((inputLine = in.readLine()) != null) {
        response += inputLine;
    }

    in.close();
    return response;
}

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);//from w  w  w  .  j ava 2  s .  c  om
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    logInfo("downloadStatus: " + conn.getResponseCode());
    return conn.getInputStream();
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {// w ww  .j  a  v a  2s . com
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(8000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {//  ww w  . j  a  va 2s. c o m
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(10000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}