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.exzogeni.dk.http.HttpTask.java

private V onPerformNetworkRequest(URI uri) throws Exception {
    Logger.debug("%s", uri);
    final HttpURLConnection cn = (HttpURLConnection) uri.toURL().openConnection();
    try {/*from  w  ww .  j a v  a2 s .  c  o m*/
        onPrepareConnectionInternal(cn);
        onPerformRequest(cn);
        return onSuccessInternal(cn);
    } finally {
        cn.disconnect();
    }
}

From source file:ext.services.network.TestNetworkUtils.java

/**
 * Test method for./*  w w  w .ja v  a  2 s .c o  m*/
 *
 * @throws Exception the exception
 * {@link ext.services.network.NetworkUtils#readURL(java.net.URLConnection, java.lang.String)}
 * .
 */
public void testReadURLURLConnectionString() throws Exception {
    HttpURLConnection connection = NetworkUtils.getConnection(URL, null);
    assertNotNull(connection);
    String str = NetworkUtils.readURL(connection, "UTF-8");
    assertNotNull(str);
    assertTrue(StringUtils.isNotBlank(str));
    connection.disconnect();
}

From source file:ext.services.network.TestNetworkUtils.java

/**
 * Test get connection url proxy with proxy.
 * //from  w  ww. j av a  2 s.  c o m
 *
 * @throws Exception the exception
 */
public void testGetConnectionURLProxyWithProxy() throws Exception {
    final ServerSocket socket = new ServerSocket(PROXY_PORT);
    Thread thread = new Thread("ProxySocketAcceptThread") {
        @Override
        public void run() {
            try {
                while (!bStop) {
                    Socket sock = socket.accept();
                    Log.debug("Accepted connection, sending back garbage and close socket...");
                    sock.getOutputStream().write(1);
                    sock.close();
                }
            } catch (IOException e) {
                Log.error(e);
            }
        }
    };
    thread.setDaemon(true); // to finish tests even if this is still running
    thread.start();
    Log.debug("Using local port: " + socket.getLocalPort());
    try {
        // useful content when inet access is allowed
        Conf.setProperty(Const.CONF_NETWORK_NONE_INTERNET_ACCESS, "false");
        HttpURLConnection connection = NetworkUtils.getConnection(new java.net.URL(URL),
                new Proxy(Type.SOCKS, "localhost", socket.getLocalPort(), "user", "password"));
        assertNotNull(connection);
        connection.disconnect();
    } finally {
        bStop = true;
        socket.close();
        thread.join();
    }
}

From source file:co.cask.cdap.internal.app.services.http.handlers.MonitorHandlerTest.java

@Test
public void testInstances() throws Exception {

    Type token = new TypeToken<Map<String, Integer>>() {
    }.getType();//from w w  w . j  a  v  a 2  s.  co m

    String path = String.format("system/services/%s/instances", Constants.Service.APP_FABRIC_HTTP);
    HttpURLConnection urlConn = openURL(path, HttpMethod.GET);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());

    Map<String, Integer> result = GSON
            .fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), token);
    Assert.assertEquals(2, result.size());
    urlConn.disconnect();
    Assert.assertEquals(1, (int) result.get("requested"));
    Assert.assertEquals(1, (int) result.get("provisioned"));
}

From source file:it.openyoureyes.test.panoramio.Panoramio.java

private Bitmap downloadFile(String url) {
    Bitmap bmImg = null;//  w ww. j  a  v a2  s .  c om
    URL myFileUrl = null;
    if (Util.isEmpty(url))
        return null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {

        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        // BitmapFactory.Options op=new BitmapFactory.Options();
        // op.inSampleSize = 3;
        bmImg = BitmapFactory.decodeStream(is);// ,null,op);
        conn.disconnect();

    } catch (IOException e) {

        e.printStackTrace();
    }
    return bmImg;
}

From source file:org.apache.servicecomb.it.schema.DownloadSchema.java

@GetMapping(path = "/netInputStream")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<InputStream> netInputStream(String content) throws IOException {
    URL url = new URL("http://localhost:" + server.getLocalPort() + "/download/netInputStream?content="
            + URLEncoder.encode(content, StandardCharsets.UTF_8.name()));
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    ResponseEntity<InputStream> responseEntity = ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=netInputStream.txt")
            .body(conn.getInputStream());
    conn.disconnect();
    return responseEntity;
}

From source file:net.ftb.util.DownloadUtils.java

/**
 * @param file - the name of the file, as saved to the repo (including extension)
 * @return - the direct link/*from ww w.  j a v a  2 s  .c  o  m*/
 */
public static String getStaticCreeperhostLink(String file) {
    String resolved = (downloadServers.containsKey(Settings.getSettings().getDownloadServer()))
            ? "http://" + downloadServers.get(Settings.getSettings().getDownloadServer())
            : Locations.masterRepo;
    resolved += "/FTB2/static/" + file;
    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) new URL(resolved).openConnection();
        connection.setRequestProperty("Cache-Control", "no-transform");
        connection.setRequestMethod("HEAD");
        if (connection.getResponseCode() != 200) {
            for (String server : downloadServers.values()) {
                if (connection.getResponseCode() != 200) {
                    resolved = "http://" + server + "/FTB2/static/" + file;
                    connection = (HttpURLConnection) new URL(resolved).openConnection();
                    connection.setRequestProperty("Cache-Control", "no-transform");
                    connection.setRequestMethod("HEAD");
                } else {
                    break;
                }
            }
        }
    } catch (IOException e) {
    }
    connection.disconnect();
    return resolved;
}

From source file:co.cask.cdap.internal.app.services.http.handlers.MonitorHandlerTest.java

@Test
public void testSystemServices() throws Exception {

    Type token = new TypeToken<List<SystemServiceMeta>>() {
    }.getType();/*  w  w  w .j a  v a 2s  . co  m*/

    HttpURLConnection urlConn = openURL("system/services", HttpMethod.GET);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
    List<SystemServiceMeta> actual = GSON
            .fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), token);

    Assert.assertEquals(8, actual.size());
    urlConn.disconnect();
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 */// w  ww  .  j  a v  a  2s. com
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        urlConnection.connect();
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } catch (IOException e) {
        String details;
        if (urlConnection != null) {
            details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage()
                    + ")";
        } else {
            details = "";
        }
        Log.e("ExoplayerUtil", "executePost: Request failed" + details, e);
        throw e;
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:co.cask.cdap.internal.app.services.http.handlers.MonitorHandlerTest.java

@Test
public void testSystemServicesStatus() throws Exception {

    Type token = new TypeToken<Map<String, String>>() {
    }.getType();//w  w  w . j a v  a2  s.c  om

    HttpURLConnection urlConn = openURL("system/services/status", HttpMethod.GET);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());

    Map<String, String> result = GSON
            .fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), token);
    Assert.assertEquals(8, result.size());
    urlConn.disconnect();
    Assert.assertEquals("OK", result.get(Constants.Service.APP_FABRIC_HTTP));
}