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:de.uni.stuttgart.informatik.ToureNPlaner.Net.Handler.SyncCoreLoader.java

private long getLastModifiedOnServer() throws IOException {
    HttpURLConnection con = null;
    long result = new Date().getTime();
    try {/*w  w w.ja  v  a  2s. co  m*/
        URL url = new URL(coreURL + pathPrefix + corePrefix + coreLevel + coreSuffix);
        con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("HEAD");
        con.setDoInput(true);
        con.setAllowUserInteraction(false);
        result = con.getHeaderFieldDate("Last-Modified", result);
        Log.d(TAG, "Last modified is parsed " + new Date(result));

    } catch (MalformedURLException e) {
        e.printStackTrace();
        return result;
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
    return result;
}

From source file:com.edduarte.vokter.job.JobManager.java

private boolean sendResponse(final String clientUrl, final String input) {
    try {// ww  w  .  ja v  a 2  s  .co m
        URL targetUrl = new URL(clientUrl);

        HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
        httpConnection.setDoOutput(true);
        httpConnection.setRequestMethod("POST");
        httpConnection.setRequestProperty("Content-Type", "application/json");

        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(input.getBytes());
        outputStream.flush();

        int responseCode = httpConnection.getResponseCode();
        httpConnection.disconnect();
        return responseCode == 200;

    } catch (IOException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return false;
}

From source file:com.bleum.canton.loadpage.LoadPage.java

private void readContentFromGet(String url) throws IOException {
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    try {/* w w  w .j  a  v  a  2 s . c  o m*/
        URL getUrl = new URL(url);
        connection = (HttpURLConnection) getUrl.openConnection();
        connection.setUseCaches(false);
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.connect();
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((reader.readLine()) != null) {
            // System.out.println(lines);
        }
    } finally {
        IOUtils.closeQuietly(reader);
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:net.sf.okapi.filters.drupal.DrupalConnector.java

public boolean updateNode(Node node) {
    try {//from   ww w .ja va  2s  .  c  om
        URL url = new URL(host + String.format("rest/node/" + node.getNid()));
        HttpURLConnection conn = createConnection(url, "PUT", true);

        OutputStream os = conn.getOutputStream();
        os.write(node.toString().getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println(conn.getResponseCode());
            System.out.println(conn.getResponseMessage());
            throw new RuntimeException("Operation failed: " + conn.getResponseCode());
        }

        conn.disconnect();
        return true;
    } catch (Throwable e) {
        throw new RuntimeException("Error in updateNode(): " + e.getMessage(), e);
    }
}

From source file:org.whispersystems.textsecure.internal.push.PushServiceSocket.java

private String makeRequest(String urlFragment, String method, String body)
        throws NonSuccessfulResponseCodeException, PushNetworkException {
    HttpURLConnection connection = makeBaseRequest(urlFragment, method, body);

    try {/*from ww w.java 2  s.  c om*/
        String response = Util.readFully(connection.getInputStream());
        connection.disconnect();

        return response;
    } catch (IOException ioe) {
        throw new PushNetworkException(ioe);
    }
}

From source file:net.sf.okapi.filters.drupal.DrupalConnector.java

public boolean postNode(Node node) {
    try {/*from  w ww.j a v  a 2 s.c o m*/
        URL url = new URL(host + String.format("rest/node"));
        HttpURLConnection conn = createConnection(url, "POST", true);

        OutputStream os = conn.getOutputStream();
        os.write(node.toString().getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println(conn.getResponseCode());
            System.out.println(conn.getResponseMessage());
            throw new RuntimeException("Operation failed: " + conn.getResponseCode());
        }

        conn.disconnect();
        return true;
    } catch (Throwable e) {
        throw new RuntimeException("Error in postNode(): " + e.getMessage(), e);
    }
}

From source file:piuk.MyRemoteWallet.java

private static String fetchURL(String URL) throws Exception {

    if (URL.indexOf("?") > 0) {
        URL += "&api_code=" + getApiCode();
    } else {/*w  w w .j  av a 2s  . c  o  m*/
        URL += "?api_code=" + getApiCode();
    }

    URL url = new URL(URL);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    try {
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestMethod("GET");

        connection.setConnectTimeout(180000);
        connection.setReadTimeout(180000);

        connection.setInstanceFollowRedirects(false);

        connection.connect();

        if (connection.getResponseCode() == 200)
            return IOUtils.toString(connection.getInputStream(), "UTF-8");
        else if (connection.getResponseCode() == 500)
            throw new Exception("Error From Server: " + IOUtils.toString(connection.getErrorStream(), "UTF-8"));
        else
            throw new Exception("Unknown response from server (" + connection.getResponseCode() + ") "
                    + IOUtils.toString(connection.getErrorStream(), "UTF-8"));

    } finally {
        connection.disconnect();
    }
}

From source file:eu.codeplumbers.cosi.api.tasks.DeleteDocumentTask.java

@Override
protected String doInBackground(Void... voids) {
    URL urlO = null;/*from ww  w.j  a  v a 2s.  c o m*/
    try {
        urlO = new URL(url + remoteId + "/");
        HttpURLConnection conn = (HttpURLConnection) urlO.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("Authorization", authHeader);
        conn.setDoOutput(false);
        conn.setDoInput(true);

        conn.setRequestMethod("DELETE");

        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());

        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        result = writer.toString();

        in.close();
        conn.disconnect();

    } catch (MalformedURLException e) {
        result = "error";
        e.printStackTrace();
        errorMessage = e.getLocalizedMessage();
    } catch (ProtocolException e) {
        result = "error";
        errorMessage = e.getLocalizedMessage();
        e.printStackTrace();
    } catch (IOException e) {
        result = "error";
        errorMessage = e.getLocalizedMessage();
        e.printStackTrace();
    }

    return result;
}

From source file:export.GarminUploader.java

private Status checkLogin() throws MalformedURLException, IOException, JSONException {
    HttpURLConnection conn = (HttpURLConnection) new URL(CHECK_URL).openConnection();
    addCookies(conn);//from w w w  .ja va  2  s. c  o  m
    {
        conn.connect();
        getCookies(conn);
        InputStream in = new BufferedInputStream(conn.getInputStream());
        JSONObject obj = parse(in);
        conn.disconnect();
        int responseCode = conn.getResponseCode();
        String amsg = conn.getResponseMessage();
        // Returns username(which is actually Displayname from profile) if
        // logged in
        if (obj.optString("username", "").length() > 0) {
            isConnected = true;
            return Uploader.Status.OK;
        } else {
            System.err.println("GarminUploader::connect() missing username, obj: " + obj.toString() + ", code: "
                    + responseCode + ", msg: " + amsg);
        }
        Status s = Status.NEED_AUTH;
        s.authMethod = Uploader.AuthMethod.USER_PASS;
        return s;
    }
}

From source file:com.ivanbratoev.festpal.datamodel.db.external.ExternalDatabaseHandler.java

private String getRemoteData(URL url, Map<String, String> parameters)
        throws ClientDoesNotHavePermissionException {
    try {//from   www  .j  a  v a  2  s .  c  om
        parameters.put(ExternalDatabaseDefinitions.PARAMETER_CLIENT, client);
        HttpURLConnection connection = setupConnection(url, parameters);
        connection.connect();
        String response = parseResponse(connection.getContent());
        connection.disconnect();
        return response;
    } catch (IOException ignore) {
        return null;
    }
}