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.gliffy.restunit.http.JavaHttp.java

/** Performs an HTTP PUT. 
 * @param request the request describing the PUT.
 * @return a response//w w w .j  ava2  s .co m
 * @throws IOException if HttpURLConnection generated an IO Exception
 */
public HttpResponse put(HttpRequest request) throws IOException {
    HttpURLConnection connection = getConnection(request);
    connection.setRequestMethod("PUT");
    connection.setDoOutput(true);
    connection.connect();
    setBody(request, connection);
    HttpResponse response = createResponse(connection);
    connection.disconnect();
    return response;
}

From source file:com.gliffy.restunit.http.JavaHttp.java

/** Performs an HTTP POST.
 * @param request the request describing the POST.
 * @return a response/*from  ww w. ja  va2  s . c o  m*/
 * @throws IOException if HttpURLConnection generated an IO Exception
 */
public HttpResponse post(HttpRequest request) throws IOException {
    HttpURLConnection connection = getConnection(request);
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.connect();
    setBody(request, connection);
    HttpResponse response = createResponse(connection);
    connection.disconnect();
    return response;
}

From source file:fr.ironcraft.assets.FileDownloader.java

private int tryGetFileSize(URL url) {
    HttpURLConnection conn = null;
    try {/*from   w  w w .  j  a  va2 s  .co  m*/
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.getInputStream();
        return conn.getContentLength();
    } catch (IOException e) {
        return -1;
    } finally {
        conn.disconnect();
    }
}

From source file:com.centurylink.mdw.ant.taskdef.HttpTransfer.java

public void uploadFile(URL destURL, File file, String userId, String password, boolean overwrite) {
    try {//from  w  w w. java2s .co m
        log("target url... " + destURL.toString());
        long fileLastModified = file.lastModified();

        HttpURLConnection conn = (HttpURLConnection) destURL.openConnection();
        long urlLastModified = conn.getLastModified();
        conn.disconnect();

        if (!overwrite && (urlLastModified >= fileLastModified)) {
            log("Destination file is up-to-date, not uploading.");
            return;
        } else {
            conn = (HttpURLConnection) destURL.openConnection();
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestMethod("PUT");
            if (userId != null) {
                String value = userId + ":" + password;
                conn.setRequestProperty("Authorization",
                        "Basic " + new String(Base64.encodeBase64(value.getBytes())));
            }

            conn.setDoOutput(true);

            OutputStream outStream = conn.getOutputStream();

            log("Uploading... " + file);

            InputStream inStream = new FileInputStream(file);

            byte[] buf = new byte[1024];
            int len = 0;
            while (len != -1) {
                len = inStream.read(buf);
                if (len > 0)
                    outStream.write(buf, 0, len);
            }

            inStream.close();
            outStream.close();
            conn.disconnect();

            int code = conn.getResponseCode();
            if (code < 200 || code >= 300) {
                String response = conn.getResponseMessage();
                throw new BuildException("Error uploading file: " + code + " -- " + response);
            }
            log("  Uploaded: " + destURL);
        }
    } catch (IOException e) {
        if (isFailOnError())
            throw new BuildException(e.getMessage(), e);
        else
            log(e.getMessage());
    }
}

From source file:com.microsoft.azure.engagement.ws.AzmeServices.java

/**
 * Method that parses the last update/*from   w  ww . j  a va  2 s. co m*/
 *
 * @return The list of feeds
 * @throws IOException, ParserConfigurationException, SAXException
 */
public final List<FeedItem> getLastUpdate(String url)
        throws IOException, ParserConfigurationException, SAXException {
    final HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();

    try {
        urlConnection.connect();
        final InputStream inputStream = urlConnection.getInputStream();
        return FeedParser.parse(inputStream);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:io.mindmaps.engine.loader.DistributedLoader.java

/**
 * Get the state of a given host/*from  w ww  .  j  ava 2s.c  o  m*/
 * @param host host to check the state of
 * @return if the given transaction has finished
 */
private String getHostState(String host) {
    HttpURLConnection connection = getHost(host, GET);
    String response = getResponseBody(connection);
    connection.disconnect();

    return response;
}

From source file:car.PasserelleRestTest.java

@Test
public void testDownloadFileKO() throws Exception {
    URL obj = new URL("http://localhost:8080/rest/api/rest/list/greterssfet.lkjsh");
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // make sure it is a get
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);
    assertTrue(con.getResponseCode() == 404);
    con.disconnect();

}

From source file:com.orchestra.portale.external.services.manager.BikeSharingService.java

@Override
public String getResponse(Map<String, String[]> mapParams) {
    try {// w ww. j a v a  2s.  co  m

        HttpURLConnection urlConnection = (HttpURLConnection) new URL(baseUrl).openConnection();
        urlConnection.setConnectTimeout(15000);
        urlConnection.setReadTimeout(30000);

        urlConnection.addRequestProperty("Accept-Language", Locale.getDefault().toString().replace('_', '-'));

        String result = IOUtils.toString(urlConnection.getInputStream());
        urlConnection.disconnect();

        return result;

    } catch (IOException e) {
        return "response{code:1,error:" + e.getMessage() + "}";
    }
}

From source file:car.PasserelleRestTest.java

/**
 * Test of downloadFile method, of class PasserelleRest.
 *//* www  . j  ava 2 s  .c  om*/
@Test
public void testDownloadFileOK() throws Exception {
    URL obj = new URL("http://localhost:8080/rest/api/rest/list/" + this.files[2].getName());
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // make sure it is a get
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);
    assertTrue(con.getResponseCode() == 200);
    con.disconnect();

}

From source file:biz.gabrys.lesscss.extended.compiler.source.HttpSource.java

public String getContent() {
    final HttpURLConnection connection = makeConnection(true);
    encoding = getEncodingFromContentType(connection.getContentType());
    String content;//from   www .  j a  va 2 s.c om
    try {
        content = IOUtils.toString(connection.getInputStream(), encoding);
    } catch (final IOException e) {
        connection.disconnect();
        throw new SourceException(e);
    }
    lastModificationDate = getModificationDate(connection.getLastModified());
    connection.disconnect();
    return content;
}