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.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPExterna() {
    String ip = null;/*ww  w .  jav  a  2  s .com*/
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        ip = baos.toString();
        baos.close();
        entrada.close();
        con.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP exterior: " + ip);
    return ip;
}

From source file:guru.benson.pinch.Pinch.java

/**
 * Handy disconnect method to wrap null-check.
 *
 * @param c/*ww w  .  j av a  2s  .  c o  m*/
 *     Connection to disconnect.
 */
private static void disconnect(HttpURLConnection c) {
    if (c != null) {
        c.disconnect();
    }
}

From source file:com.meetingninja.csse.database.ProjectDatabaseAdapter.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 w w . ja va 2  s.c  o  m

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

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPServidorRemoto() {
    String ip = null;//from   w w  w.  j  a va2s.  co  m
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        JSONObject json = new JSONObject(baos.toString());
        baos.close();
        entrada.close();
        con.disconnect();
        ip = json.getString("content");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP servidor remoto: " + ip);
    return ip;
}

From source file:lu.list.itis.dkd.aig.util.FusekiHttpHelper.java

/**
 * Delete specified dataset/*from w w  w  . j a va 2 s  . c om*/
 * 
 * @param dataSetName
 * @throws IOException
 * @throws HttpException
 */
public static void deleteDataSet(@NonNull String dataSetName) throws IOException, HttpException {
    logger.info("delete dataset: " + dataSetName);

    URL url = new URL(HOST + "/$/datasets/" + dataSetName);
    final HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    httpConnection.setUseCaches(false);
    httpConnection.setRequestMethod("DELETE");
    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    // handle HTTP/HTTPS strange behaviour
    httpConnection.connect();
    httpConnection.disconnect();

    // handle response
    switch (httpConnection.getResponseCode()) {
    case HttpURLConnection.HTTP_OK:
        break;
    default:
        throw new HttpException(
                httpConnection.getResponseCode() + " message: " + httpConnection.getResponseMessage());
    }
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static String postRESTful(String RESTfull_URL, String data) {
    String state = "";
    try {//ww  w.  j a v  a 2 s.  com
        URL url = new URL(RESTfull_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        postRequest(connection, data);
        //Get Response
        state = postResponse(connection);

        if (connection != null) {
            connection.disconnect();
        }
    } catch (Exception ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }
    System.out.println(state);
    return state;
}

From source file:org.apache.felix.http.itest.BaseIntegrationTest.java

protected static void assertResponseCode(int expected, URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try {// w ww  .  j  a v a  2s  .  c o  m
        assertEquals(expected, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}

From source file:com.wx.kernel.util.HttpKit.java

/**
 * Send GET request//from ww w . j  ava2 s.  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 (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:de.forsthaus.backend.util.IpLocator.java

/**
 * Gets the content for a given url. This method makes a connection, gets
 * the response from the url./*w w  w. ja  v  a  2  s .  c om*/
 * 
 * A RuntimeException is throws is the status code of the response is not
 * 200.
 * 
 * @param url
 *            The url to open.
 * @return HTML response
 * @throws IOException
 */
private static List<String> getContent(URL url) throws IOException {

    final HttpURLConnection http = (HttpURLConnection) url.openConnection();
    try {
        http.connect();

        final int code = http.getResponseCode();
        if (code != 200)
            throw new IOException(
                    "IP Locator failed to get the location. Http Status code : " + code + " [" + url + "]");
        return getContent(http);
    } finally {
        http.disconnect();
    }
}

From source file:flexpos.restfulConnection.java

public static String postRESTful(String RESTfull_URL, String data) {
    String state = "";
    try {//  w  ww  .  j a v a  2 s . c o  m
        URL url = new URL(RESTfull_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        postRequest(connection, data);

        //Get Response
        state = postResponse(connection);

        if (connection != null) {
            connection.disconnect();
        }
    } catch (Exception ex) {
        Logger.getLogger(restfulConnection.class.getName()).log(Level.SEVERE, null, ex);
    }

    return state;
}