Example usage for java.net URLConnection connect

List of usage examples for java.net URLConnection connect

Introduction

In this page you can find the example usage for java.net URLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:org.apache.hadoop.hbase.http.log.LogLevel.java

private static void process(String urlstring) {
    try {//  ww  w . j a v a 2s . c  om
        URL url = new URL(urlstring);
        System.out.println("Connecting to " + url);
        URLConnection connection = url.openConnection();
        connection.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        for (String line; (line = in.readLine()) != null;)
            if (line.startsWith(MARKER)) {
                System.out.println(TAG.matcher(line).replaceAll(""));
            }
        in.close();
    } catch (IOException ioe) {
        System.err.println("" + ioe);
    }
}

From source file:com.handlerexploit.news.utils.BitmapLruCache.java

/**
 * Convenience method to retrieve a bitmap image from a URL over the
 * network. The built-in methods do not seem to work, as they return a
 * FileNotFound exception.//from w w  w. j a  v  a 2s .c  o  m
 * 
 * Note that this does not perform any threading -- it blocks the call while
 * retrieving the data.
 * 
 * @param url
 *            The URL to read the bitmap from.
 * @return A Bitmap image or null if an error occurs.
 */
public static Bitmap readBitmapFromNetwork(String url) {
    InputStream inputStream = null;
    BufferedInputStream bufferedInputStream = null;
    Bitmap bitmap = null;

    try {
        URLConnection conn = new URL(url).openConnection();
        conn.connect();
        inputStream = conn.getInputStream();
        bufferedInputStream = new BufferedInputStream(inputStream, 8192);
        bitmap = BitmapFactory.decodeStream(bufferedInputStream);
    } catch (OutOfMemoryError e) {
        Log.d(TAG, "Ran out of memory.", e);
    } catch (Throwable e) {
        if (!e.getClass().equals(UnknownHostException.class)) {
            Log.d(TAG, "Could not get remote image : " + e.getClass().getSimpleName(), e);
        }
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
            }
        } catch (IOException e) {
            Log.d(TAG, "Error closing stream.", e);
        }
    }
    return bitmap;
}

From source file:Main.java

public static String ParserHtml(String url) {
    String html = "";
    BufferedReader in = null;// w ww .j  a va 2 s.c o  m
    try {
        URL realUrl = new URL(url);
        URLConnection connection = realUrl.openConnection();
        connection.setRequestProperty("User-agent", "Mozilla/5.0");
        connection.connect();
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            html += line;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return html;
}

From source file:fr.ign.cogit.geoxygene.appli.gl.program.GLProgramBuilder.java

private static boolean testURLValid(URL url) throws IOException {
    URLConnection huc = url.openConnection();
    try {//from   w ww  . jav a  2 s .c  o  m
        huc.connect();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.apache.hadoop.hbase.http.TestGlobalFilter.java

/** access a url, ignoring some IOException such as the page does not exist */
static void access(String urlstring) throws IOException {
    LOG.warn("access " + urlstring);
    URL url = new URL(urlstring);
    URLConnection connection = url.openConnection();
    connection.connect();

    try {/*from w  w  w  . j a v a  2  s . com*/
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        try {
            for (; in.readLine() != null;)
                ;
        } finally {
            in.close();
        }
    } catch (IOException ioe) {
        LOG.warn("urlstring=" + urlstring, ioe);
    }
}

From source file:org.apache.hadoop.hbase.http.TestPathFilter.java

/** access a url, ignoring some IOException such as the page does not exist */
static void access(String urlstring) throws IOException {
    LOG.warn("access " + urlstring);
    URL url = new URL(urlstring);

    URLConnection connection = url.openConnection();
    connection.connect();

    try {/*from   w ww .  j a v  a  2s. c om*/
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        try {
            for (; in.readLine() != null;)
                ;
        } finally {
            in.close();
        }
    } catch (IOException ioe) {
        LOG.warn("urlstring=" + urlstring, ioe);
    }
}

From source file:cc.vidr.datum.util.FreebaseUtils.java

/**
 * Return the list of JSON objects returned by the API for the given query.
 * /*from  ww  w.  j a v a2  s.  c o m*/
 * @param path  the API service path
 * @return      the list of JSON objects
 * @throws      IOException if there was an error communicating with the
 *              server
 * @throws      ParseException if the server returns malformed JSON
 */
@SuppressWarnings("unchecked")
public static List<JSONObject> getResultList(String path) throws IOException, ParseException {
    URLConnection connection = new URL(endpoint + path).openConnection();
    connection.connect();
    InputStream stream = connection.getInputStream();
    Reader reader = new InputStreamReader(stream);
    JSONObject o = (JSONObject) JSONValue.parse(reader);
    return (List<JSONObject>) o.get("result");
}

From source file:com.pentaho.ctools.utils.HttpUtils.java

/**
 * This method shall return the status of HTTP request.
 *
 * @param url//w w  w .j a  va  2s  .  c  o m
 * @return
 * @throws Exception
 */
public static int GetHttpStatus(String url) {
    LOG.debug("The URL: " + url);
    int nHttpStatus = HttpStatus.SC_BAD_REQUEST;

    try {
        URL oUrl = new URL(url);
        URLConnection uc = oUrl.openConnection();
        uc.connect();
        nHttpStatus = ((HttpURLConnection) uc).getResponseCode();
        LOG.debug("HTTP Status:" + nHttpStatus);
    } catch (Exception ex) {
        LOG.error(ex.getMessage());
    }

    return nHttpStatus;
}

From source file:mc.lib.network.NetworkHelper.java

public static Bitmap getBitmap(String url) {
    Bitmap res = null;//from   w  w w.j  a v a  2  s .  c  o m
    InputStream is = null;
    try {
        URLConnection c = new URL(url).openConnection();
        c.connect();
        is = c.getInputStream();
        res = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        Log.e(LOGTAG, "Wrong url format", e);
    } catch (Exception e) {
        Log.e(LOGTAG, "Cannot get bitmap", e);
    } finally {
        StreamHelper.close(is);
    }
    return res;
}

From source file:mc.lib.network.NetworkHelper.java

public static void saveToFile(String url, File file) {
    InputStream is = null;/*from  ww w .j  a va  2s.  c o m*/
    OutputStream os = null;
    try {
        URLConnection c = new URL(url).openConnection();
        c.connect();
        is = c.getInputStream();
        os = new FileOutputStream(file);
        StreamHelper.copyNetworkStream(is, os, c.getReadTimeout());
    } catch (MalformedURLException e) {
        Log.e(LOGTAG, "Wrong url format", e);
    } catch (Exception e) {
        Log.e(LOGTAG, "Can not open stream", e);
    } finally {
        StreamHelper.close(os);
        StreamHelper.close(is);
    }
}