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.cyphermessenger.client.SyncRequest.java

public static List<String> findUser(CypherSession session, String username, int limit)
        throws IOException, APIErrorException {
    String[] keys = new String[] { "username", "limit" };
    String[] vals = new String[] { username, limit + "" };

    HttpURLConnection conn = doRequest("find", session, keys, vals);

    if (conn.getResponseCode() != 200) {
        throw new IOException("Server error");
    }/*from w ww .  j a  va 2 s.  c o  m*/

    JsonNode node = MAPPER.readTree(conn.getInputStream());
    conn.disconnect();
    int statusCode = node.get("status").asInt();
    if (statusCode != StatusCode.OK) {
        throw new APIErrorException(statusCode);
    } else {
        return Utils.MAPPER.treeToValue(node.get("users"), ArrayList.class);
    }
}

From source file:Main.java

/**
 * Downloads a file via HTTP(S) GET to the given path. This function cannot be called from the
 * UI thread. Android does not allow it.
 *
 * @param urlString Url to the ressource to download.
 * @param file      file to be written to.
 * @param overwrite if file exists, overwrite?
 * @return flase if download was not successful. If successful, true.
 *///from  w  ww  .  ja v  a2  s. c  om
private static Boolean fileDownloadHttp(String urlString, File file, Boolean overwrite) {
    HashMap<String, String> result = null;
    URL url = null;
    //        File temp;

    try {
        url = new URL(urlString);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(200000);
        urlConnection.connect();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        FileOutputStream outputStream = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        in.close();
        outputStream.close();
        urlConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    Log.d(TAG, "File download: " + file.getAbsolutePath() + url.getFile() + "overwrite " + overwrite
            + "exists? " + file.exists());
    return true;
}

From source file:Main.java

static public String download_json(String url_addr) {
    StringBuilder result = new StringBuilder();
    try {/*  w  w w .ja  va  2s . c om*/
        URL url = new URL(url_addr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if (conn != null) {
            conn.setConnectTimeout(10000);
            conn.setUseCaches(false);
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                for (;;) {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    result.append(line + '\n');
                }
                br.close();
            } else {
                conn.disconnect();
                return null;
            }
            conn.disconnect();
        }
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        return null;
    }
    return result.toString();
}

From source file:Main.java

public static String getJsonWithPath(String path) {
    StringBuffer sb = new StringBuffer();
    URL url = null;/*from w  ww  .j a va 2 s. c  o  m*/
    HttpURLConnection conn = null;
    BufferedReader br = null;

    try {
        url = new URL(path);
        conn = (HttpURLConnection) url.openConnection();
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String temp = "";
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
        } else {
            Log.e(TAG, " NET IS ERROR");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(br);
        conn.disconnect();
    }
    Log.i("TAG", "---" + sb.toString());
    return sb.toString();
}

From source file:com.cyphermessenger.client.SyncRequest.java

public static Captcha requestCaptcha() throws IOException, APIErrorException {
    HttpURLConnection conn = doRequest("captcha");
    if (conn.getResponseCode() != 200) {
        throw new IOException("Server error");
    }/*w  ww  . j  av  a2  s .c  om*/
    InputStream in = conn.getInputStream();
    JsonNode node = MAPPER.readTree(in);
    conn.disconnect();
    int statusCode = node.get("status").asInt();
    String captchaTokenString = node.get("captchaToken").asText();
    String captchaHashString = node.get("captchaHash").asText();
    String captchaImageString = node.get("captchaBytes").asText();

    byte[] captchaHash = Utils.BASE64_URL.decode(captchaHashString);
    byte[] captchaImage = Utils.BASE64_URL.decode(captchaImageString);
    if (statusCode == StatusCode.OK) {
        return new Captcha(captchaTokenString, captchaHash, captchaImage);
    } else {
        throw new APIErrorException(statusCode);
    }
}

From source file:Main.java

public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*from  w  w  w . j ava2  s .  c o m*/
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:com.quackware.handsfreemusic.Utility.java

public static String getSourceCode(URL url) {
    Object content = null;/*w  w w . j  a v  a  2  s  .  c o m*/
    try {

        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4");
        uc.connect();
        InputStream stream = uc.getInputStream();
        if (stream != null) {
            content = readStream(uc.getContentLength(), stream);
        } else if ((content = uc.getContent()) != null && content instanceof java.io.InputStream)
            content = readStream(uc.getContentLength(), (java.io.InputStream) content);
        uc.disconnect();

    } catch (Exception ex) {
        return null;
    }
    if (content != null && content instanceof String) {
        String html = (String) content;
        return html;
    } else {
        return null;
    }
}

From source file:io.github.retz.web.JobRequestRouter.java

public static boolean statHTTPFile(String url, String name) {
    String addr = url.replace("files/browse", "files/download") + "%2F" + maybeURLEncode(name);

    HttpURLConnection conn = null;
    try {/*from   ww w . j  a va2s  .c  om*/
        conn = (HttpURLConnection) new URL(addr).openConnection();
        conn.setRequestMethod("HEAD");
        conn.setDoOutput(false);
        LOG.debug(conn.getResponseMessage());
        return conn.getResponseCode() == 200 || conn.getResponseCode() == 204;
    } catch (IOException e) {
        LOG.debug("Failed to fetch {}: {}", addr, e.toString());
        return false;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:com.cyphermessenger.client.SyncRequest.java

public static void userLogout(CypherSession session) throws IOException, APIErrorException {
    HttpURLConnection conn = doRequest("logout", session, null);

    if (conn.getResponseCode() != 200) {
        throw new IOException("Server error");
    }/* w w  w  .  j  a  v a2 s.com*/

    InputStream in = conn.getInputStream();
    JsonNode node = MAPPER.readTree(in);
    conn.disconnect();
    int statusCode = node.get("status").asInt();
    if (statusCode != StatusCode.OK) {
        throw new APIErrorException(statusCode);
    }
}

From source file:com.mopaas_mobile.http.BaseHttpRequester.java

public static String doGETwithHeader(String urlstr, String token, List<BasicNameValuePair> params)
        throws IOException {
    String result = null;//from ww  w. j  a  v a  2s .  co  m
    String content = "";
    for (int i = 0; i < params.size(); i++) {
        content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "="
                + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8");
    }
    URL url = new URL(urlstr + "?" + content.substring(1));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("token", token);
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.connect();
    InputStream is = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    StringBuffer b = new StringBuffer();
    int ch;
    while ((ch = br.read()) != -1) {
        b.append((char) ch);
    }
    result = b.toString().trim();
    connection.disconnect();
    return result;
}