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 String getJsonContent(String url_path) {
    try {/*  w  w w. j ava2s.  c o  m*/
        URL url = new URL(url_path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        int code = connection.getResponseCode();
        if (code == 200) {
            return changeInputStream(connection.getInputStream());
        }
    } catch (Exception e) {

    }
    return "";
}

From source file:Main.java

/**
 * Send the Http's request with the address of url
 * @param String url//from  w w  w.ja  v  a 2s. c om
 * @return String
 */
public static String getHttpRequest(String url) {
    //should use the StringBuilder or StringBuffer, String is not used.
    StringBuffer jsonContent = new StringBuffer();
    try {
        URL getUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        connection.connect();
        //Getting the inputting stream, and then read the stream.
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String lines = "";
        while ((lines = reader.readLine()) != null) {
            jsonContent.append(lines);
        }
        reader.close();
        connection.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
    //ScanningActivity.log("getHttpRequest(): " + content);
    //BooksPutIn.log("getHttpRequest(): " + content);
    return jsonContent.toString();
}

From source file:Main.java

public static long downloadFileFromUrl(String urlPath, File file) {
    long size = 0;
    try {/*ww  w  .j  ava2  s  . c  o m*/
        URL url = new URL(urlPath);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream());
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
        int i;
        while ((i = bufferedinputstream.read()) != -1) {
            bufferedoutputstream.write(i);
        }
        bufferedinputstream.close();
        bufferedoutputstream.close();
        httpurlconnection.disconnect();
        size = file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlStr) {
    Bitmap bitmap = null;/*from   ww  w  .  j  a  v a2s.  com*/
    try {
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        Log.e(LOG_TAG, "cannot get the image from link :" + urlStr);
    }
    return bitmap;
}

From source file:com.bdaum.zoom.net.core.internal.Activator.java

public static InputStream openStream(String url, int timeout)
        throws MalformedURLException, IOException, HttpException {
    URL u = new URL(url);
    URLConnection con = u.openConnection();
    con.setConnectTimeout(timeout);//  w w w . j av  a 2  s  . co m
    con.setReadTimeout(timeout);
    try {
        return new BufferedInputStream(con.getInputStream());
    } catch (IOException e) {
        int responseCode = -1;
        if (con instanceof HttpURLConnection)
            responseCode = ((HttpURLConnection) con).getResponseCode();
        else if (con instanceof HttpsURLConnection)
            responseCode = ((HttpsURLConnection) con).getResponseCode();
        if (responseCode >= 0 && responseCode != HttpStatus.SC_OK)
            throw new HttpException(getStatusText(responseCode), e);
        throw e;
    }
}

From source file:Main.java

public static String SimpleHttp(String urlStr) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    int resCode = conn.getResponseCode();
    InputStream input = null;// ww w  . j  a v  a2  s  .com
    String result = null;
    if (resCode == 200) {
        input = conn.getInputStream();
        result = toString(input);
        input.close();
        conn.disconnect();
    } else {
        throw new Exception("connect failed");
    }

    return result;
}

From source file:net.foxgenesis.helper.SiteReader.java

private static BufferedReader getStream(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", "X-ServerStats");
    connection.setReadTimeout(5000);//  www.  j a v  a2s .  c  o  m
    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
}

From source file:Main.java

public static String request(String httpUrl, String httpArg) {
    BufferedReader reader = null;
    String result = null;//from  w w  w  .  j a  v  a 2  s  . c o m
    StringBuffer sbf = new StringBuffer();
    httpUrl = httpUrl + "?" + httpArg;

    try {
        URL url = new URL(httpUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("apikey", "2ffbcae4d025b6c109af30d7de2d7c09");
        connection.connect();
        InputStream is = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String strRead = null;
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
            sbf.append("\r\n");
        }
        reader.close();
        result = sbf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String accessToFlashAir(String uri) throws IOException {
    URL url = new URL(uri);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    String result = null;/*www .ja  v  a  2s. c  o  m*/
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        result = inputStreamToString(in);
        in.close();
    } finally {
        urlConnection.disconnect();
    }

    return result;
}

From source file:Main.java

public static String readTextFromURL(String urlString) throws IOException {

    HttpURLConnection urlConnection = null;

    URL url = new URL(urlString);

    urlConnection = (HttpURLConnection) url.openConnection();

    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000 /* milliseconds */);
    urlConnection.setConnectTimeout(15000 /* milliseconds */);

    urlConnection.setDoOutput(true);/*w  ww. j a  v a 2s.com*/

    urlConnection.connect();

    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

    char[] buffer = new char[1024];

    String jsonString = new String();

    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    br.close();

    return sb.toString();
}