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.meetingninja.csse.database.BaseDatabaseAdapter.java

protected static String updateHelper(String jsonPayload) throws IOException {
    // Server URL setup
    String _url = getBaseUri().build().toString();

    // Establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    // add request header
    conn.setRequestMethod(IRequest.PUT);
    addRequestHeader(conn, true);/*from   w ww.j a va  2 s.  c o m*/

    int responseCode = sendPostPayload(conn, jsonPayload);
    String response = getServerResponse(conn);
    conn.disconnect();
    return response;
}

From source file:Main.java

private static boolean confirmDownload(Context context, String stringUrl) {
    try {/*from   w w  w.  ja v  a2 s  . com*/
        URL url = new URL(stringUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        long candidateDate = urlConnection.getLastModified();
        final SharedPreferences prefsManager = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext());

        final long savedDate = prefsManager.getLong(stringUrl, 0);

        urlConnection.disconnect();
        if (candidateDate <= savedDate) {
            return false;
        }
        timeToConfirm = candidateDate;

        return true;

    } catch (Throwable localThrowable) {

        localThrowable.printStackTrace();
        return false;

    }
}

From source file:Main.java

public static String customrequestget(String url, HashMap<String, String> map, String method) {

    if (null != map) {
        int i = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {

            if (i == 0) {
                url = url + "?" + entry.getKey() + "=" + entry.getValue();
            } else {
                url = url + "&" + entry.getKey() + "=" + entry.getValue();
            }/* w  w w. j a  va2 s. co m*/

            i++;
        }
    }
    try {

        URL murl = new URL(url);
        System.out.print(url);
        HttpURLConnection conn = (HttpURLConnection) murl.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod(method);

        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");

        InputStream inStream = conn.getInputStream();
        String result = inputStream2String(inStream);
        conn.disconnect();
        return result;
    } catch (Exception e) {
        // TODO: handle exception
    }
    return null;
}

From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Leer contenido texto UTF-8 desde el URL dado
 *
 * @param _url el URL a leer// w w  w.jav  a2 s . c o  m
 * @return el contenido del URL como String
 * @throws Exception
 */
public static String read(String _url) throws Exception {

    StringBuilder b = new StringBuilder();
    try {

        //logger.info("HTTP request: " + _url);
        final URL url = new URL(_url);
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
        b.append(slurp(connection.getInputStream(), 1024));

        connection.disconnect();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return b.toString();

}

From source file:Main.java

private static String getStringFromUrl(String url, int connectTimeout, int readTimeout)
        throws MalformedURLException, JSONException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
    String jsonString = "";

    if (connectTimeout != 0) {
        urlConn.setConnectTimeout(connectTimeout);
    }//from  ww w .j a v a  2  s  .  com
    if (readTimeout != 0) {
        urlConn.setReadTimeout(readTimeout);
    }

    try {
        jsonString = getStringFromInputStream(urlConn.getInputStream());
    } finally {
        urlConn.disconnect();
    }
    return jsonString;
}

From source file:Main.java

private static String connect(String uri, String charsetName) {
    String result = "";
    try {/*from   ww w .j  a v  a 2s. c  o m*/
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        if (Integer.parseInt(Build.VERSION.SDK) < 8) {
            System.setProperty("http.keepAlive", "false");
        }
        if (conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            result = readData(is, charsetName);
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (Exception e) {
    }
    return result;
}

From source file:jfix.util.Urls.java

/**
 * Returns true if given url can be connected via HTTP within given timeout
 * (specified in seconds). Otherwise the url might be broken.
 *//*from  ww  w .ja  v a2 s .  co  m*/
public static boolean isConnectable(String url, int timeout) {
    try {
        URLConnection connection = new URL(url).openConnection();
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setConnectTimeout(timeout * 1000);
            httpConnection.setReadTimeout(timeout * 1000);
            httpConnection.connect();
            int response = httpConnection.getResponseCode();
            httpConnection.disconnect();
            return response == HttpURLConnection.HTTP_OK;
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:jfix.util.Urls.java

/**
 * Returns the status code for connecting given url with given timeout.
 * Returns 0 if an IOException occurs./*w  w w  . ja  v a2s  .  co m*/
 */
public static int getStatus(String url, int timeout) {
    try {
        URLConnection connection = new URL(url).openConnection();
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setConnectTimeout(timeout * 1000);
            httpConnection.setReadTimeout(timeout * 1000);
            httpConnection.connect();
            int response = httpConnection.getResponseCode();
            httpConnection.disconnect();
            return response;
        }
    } catch (IOException e) {
        // pass
    }
    return 0;
}

From source file:com.adguard.compiler.UrlUtils.java

public static String downloadString(URL url, String encoding) throws IOException {

    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {/*www . j  a  va2s  .  co  m*/
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        inputStream = connection.getInputStream();
        return IOUtils.toString(inputStream, encoding);
    } finally {
        IOUtils.closeQuietly(inputStream);
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:com.jeffrodriguez.webtools.client.URLConnectionWebClientImpl.java

private static <T> T disconnectAndReturn(HttpURLConnection connection, T result) throws IOException {
    connection.disconnect();
    return result;
}