Example usage for java.net HttpURLConnection disconnect

List of usage examples for java.net HttpURLConnection disconnect

Introduction

In this page you can find the example usage for java.net HttpURLConnection disconnect.

Prototype

public abstract void disconnect();

Source Link

Document

Indicates that other requests to the server are unlikely in the near future.

Usage

From source file:com.gabrielluz.megasenaanalyzer.MegaSenaAnalyzer.java

public static byte[] downloadFile(String fileUrl) throws IOException {
    java.net.CookieManager cm;/*ww w .  j a v a  2s .  c  om*/
    cm = new java.net.CookieManager();
    java.net.CookieHandler.setDefault(cm);
    URL url = new URL(fileUrl);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    int responseCode = httpConn.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        try (InputStream inputStream = httpConn.getInputStream()) {
            return MegaSenaAnalyzer.unZipIt(inputStream);
        }
    } else {
        System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        httpConn.disconnect();
        return null;
    }
}

From source file:org.apache.brooklyn.util.http.HttpTool.java

/**
 * Closes all streams of the connection, and disconnects it. Ignores all exceptions completely,
 * not even logging them!/*  w w w.  j  a v  a 2 s  .c om*/
 */
public static void closeQuietly(HttpURLConnection connection) {
    try {
        connection.disconnect();
    } catch (Exception e) {
    }
    try {
        connection.getInputStream().close();
    } catch (Exception e) {
    }
    try {
        connection.getOutputStream().close();
    } catch (Exception e) {
    }
    try {
        connection.getErrorStream().close();
    } catch (Exception e) {
    }
}

From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesUtility.java

protected static int insertDataPoints(String urlString, List<IncomingDataPoint> points) throws IOException {
    int code = 0;
    Gson gson = new Gson();

    HttpURLConnection httpConnection = TimeSeriesUtility.openHTTPConnectionPOST(urlString);
    OutputStreamWriter wr = new OutputStreamWriter(httpConnection.getOutputStream());

    String json = gson.toJson(points);

    wr.write(json);//from  w ww .  j av  a 2s .c  o m
    wr.flush();
    wr.close();

    code = httpConnection.getResponseCode();

    httpConnection.disconnect();

    return code;
}

From source file:io.hops.hopsworks.api.tensorflow.TensorboardProxyServlet.java

public static String getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;//ww w. j av a2s .c om
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    conn.disconnect();
    return result.toString();
}

From source file:jmc.util.UtlFbComents.java

public static String getJSONComentarios(String url, Long numComents) throws JMCException {
    String linea = "";
    String buf = "";

    try {//from www  .j  a va2s .c  o  m
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL(
                "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));

        while ((linea = br.readLine()) != null) {
            buf.concat(linea);
        }
        cn.disconnect();

    } catch (IOException e) {
        throw new JMCException(e);
    }
    return buf;
}

From source file:Main.java

public static Bitmap getImageFromURL(URL url) {

    Bitmap bitmap = null;/*from   w ww . ja  v  a  2 s .co m*/

    HttpURLConnection connection = null;

    try {

        connection = (HttpURLConnection) url.openConnection();

        if (connection.getResponseCode() != 200) {
            return null;
        }
        if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) {
            return null;
        }

        bitmap = BitmapFactory.decodeStream(connection.getInputStream());

    } catch (IOException e) {

        e.printStackTrace();

    }

    if (connection != null) {

        connection.disconnect();

    }

    return bitmap;

}

From source file:com.whp.android.location.geocode.GeocodeHelper.java

/**
 * getAddresses/*from www. jav  a  2  s  .c o m*/
 * 
 * @param url
 * @return
 */
public static GeocodeResult getAddresses(StringBuilder url) {
    GeocodeResult result = null;

    try {

        HttpURLConnection httpURLConnection = null;
        StringBuilder stringBuilder = new StringBuilder();

        httpURLConnection = (HttpURLConnection) new URL(url.toString()).openConnection();
        InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());

        int read;
        char[] buff = new char[1024];
        while ((read = inputStreamReader.read(buff)) != -1) {
            stringBuilder.append(buff, 0, read);
        }
        httpURLConnection.disconnect();

        JSONObject jObject = new JSONObject(stringBuilder.toString());

        result = GeocodeJsonParser.parse(jObject);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

@SuppressWarnings("unused")
public static void selectServer2() {
    for (String host : host_arr) {
        HttpURLConnection conn = null;
        try {//from w w  w  . j a  v a 2 s . c  om
            conn = (HttpURLConnection) new URL(host).openConnection();
            conn.setConnectTimeout(6000);
            int result = conn.getResponseCode();
            String re = conn.getResponseMessage();
            if (result == HttpURLConnection.HTTP_OK) {
                HOST = host;//
                break;
            } else {
            }
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } finally {
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }

    }
}

From source file:com.wisdombud.right.client.common.HttpKit.java

/**
 * Send GET request/* w w w  .  j  ava 2s . c  o  m*/
 */
public static String get(String url, Map<String, String> queryParas, Map<String, String> headers) {
    HttpURLConnection conn = null;
    try {
        conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), GET, headers);
        conn.connect();
        return readResponseString(conn);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:com.example.admin.processingboilerplate.JsonIO.java

public static StringBuilder loadString(String url) {
    StringBuilder sb = new StringBuilder();
    try {/*from  w w  w .j a  va2 s  .  c o m*/
        URLConnection uc = (new URL(url)).openConnection();
        HttpURLConnection con = url.startsWith("https") ? (HttpsURLConnection) uc : (HttpURLConnection) uc;
        try {
            con.setReadTimeout(6000);
            con.setConnectTimeout(6000);
            con.setRequestMethod("GET");
            con.setDoInput(true);
            con.setRequestProperty("User-Agent", USER_AGENT);
            sb = load(con);
        } catch (IOException e) {
            Log.e("loadJson", e.getMessage(), e);
        } finally {
            con.disconnect();
        }
    } catch (IOException e) {
        Log.e("loadJson", e.getMessage(), e);
    }
    return sb;
}