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:commonUtils.CommonUtils.java

public static boolean IsExistsLink(String inputLink) {
    try {/*from   ww  w . j  a  va 2s.com*/
        URL url = new URL(inputLink);
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        System.out.println(huc.getResponseCode());
        if (huc.getResponseCode() == 200) {
            return true;
        }

    } catch (Exception ex) {
        return false;
    }
    return false;
}

From source file:javarestart.Utils.java

private static String getText(final URL url) throws IOException {
    final URLConnection connection = url.openConnection();
    try (LineNumberReader in = new LineNumberReader(
            new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")))) {

        final StringBuilder response = new StringBuilder();
        String inputLine;/*from   w w w.  ja v  a2s.  co m*/
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        return response.toString();
    }
}

From source file:Main.java

/**
 * This method fetches data from a given url
 *
 * @param strUrl Url from which the data will be fetched
 * @return A String representing the resource obtained in the connection
 * @throws IOException If something went wrong with the connection
 *//*from  w  w  w.  j a  v  a2  s. c  o m*/
public static String getDataFromUrl(String strUrl) throws IOException {
    InputStream iStream;
    HttpURLConnection urlConnection;
    URL url = new URL(strUrl);

    // Creating an http connection to communicate with url
    urlConnection = (HttpURLConnection) url.openConnection();

    // Connecting to url
    urlConnection.connect();

    // Reading data from url
    iStream = urlConnection.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

    StringBuilder sb = new StringBuilder();

    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    br.close();
    iStream.close();
    urlConnection.disconnect();
    return sb.toString();
}

From source file:Main.java

public static InputStream getHTTPInputStream(String url) {
    try {// w w  w  .  j  a  v  a  2  s . 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:com.tnc.android.graphite.utils.GraphiteConnection.java

public static DrawableGraph getGraph(String serverUrl, String ps) throws Exception {
    URL url = new URL(serverUrl + GRAPH_PARAM_STRING + ps);
    HttpURLConnection http = (HttpURLConnection) url.openConnection();
    http.setConnectTimeout(45000);/*from  ww  w  . ja va 2s.c  om*/
    http.setReadTimeout(45000);
    Drawable image = Drawable.createFromStream(http.getInputStream(), null);
    DrawableGraph dg = new DrawableGraph();
    dg.setImage(image);
    return dg;
}

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 {/*  w w  w  . j  a  v  a2 s . c  o  m*/
        huc.connect();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:$.FileUtils.java

public static void urlToPath(URL url, File fOut) throws IOException {
        URLConnection uc = url.openConnection();
        logger.info("ContentType: " + uc.getContentType());
        InputStream in = uc.getInputStream();
        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
        logger.info("File of length " + fOut.length() + " created from URL " + url.toString());
        in.close();/*  w  w  w.ja v  a 2s. c om*/
    }

From source file:com.publicuhc.pluginframework.util.UUIDFetcher.java

protected static HttpURLConnection getConnection() throws IOException {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");

    connection.setUseCaches(false);//from  ww w.j a va2 s  . c  o m
    connection.setDoInput(true);
    connection.setDoOutput(true);

    return connection;
}

From source file:dataHandlers.AccessFacebook.java

private static List<String> extractArtistFromPost(List<Post> postList, User user) {
    List<String> recentArtistList = new ArrayList<>();
    int postCount = 0;
    int maxPostCount = GlobalParam.getPostCountPerUser();
    LOGGER.log(Level.WARNING, "Some music information may not contain artist information");
    for (Post post : postList) {
        String stringURL = BASE_URL + post.getId() + "/attachments?access_token="
                + ((UserFacebook) user).getAccessToken();
        try {/*ww w  .  j  a  va  2 s  .c om*/
            URL url = new URL(stringURL);
            URLConnection respons = url.openConnection();
            String stringRespons = responsToString(respons);
            JSONArray jArray = new JSONArray(stringRespons.substring(8, stringRespons.length() - 1));
            JSONObject dataObject = jArray.getJSONObject(0);
            recentArtistList.add(dataObject.getString("description"));
            postCount++;
        } catch (IOException e) {
            LOGGER.log(Level.INFO, "Responce for " + post.getId() + " is broken or unavailable");
        } catch (Exception e) {
            LOGGER.log(Level.INFO, "Post " + post.getId() + " dose not contains any aritist information");
        }
    }
    return recentArtistList;
}

From source file:be.roots.taconic.pricingguide.util.HttpUtil.java

private static HttpURLConnection getInputStreamFor(String urlAsString, String userName, String password)
        throws IOException {
    LOGGER.info(/* ww  w .  jav a2 s  . c om*/
            REQUEST_METHOD + "ting data from url: " + urlAsString + " with timeout set to " + CONNECT_TIMEOUT);

    final URL url = new URL(urlAsString);
    final HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod(REQUEST_METHOD);
    con.setConnectTimeout(CONNECT_TIMEOUT);

    if (userName != null || password != null) {
        final String encoded = Base64.encode(userName + ":" + password);
        con.setRequestProperty("Authorization", "Basic " + encoded);
    }

    final int responseCode = con.getResponseCode();

    LOGGER.info("Response code: " + responseCode);
    return con;
}