Java Utililty Methods URL Read

List of utility methods to do URL Read

Description

The list of methods to do URL Read are organized into topic(s).

Method

StringgetBodyResponse(HttpURLConnection con)
get Body Response
int responseCode = con.getResponseCode();
InputStream stream;
if (responseCode / 100 == 2) {
    stream = con.getInputStream();
} else {
    stream = con.getErrorStream();
if (stream != null) {
...
byte[]getBytes(URL url)
get Bytes
URLConnection uc = url.openConnection();
if (uc instanceof java.net.HttpURLConnection) {
    java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
    int code = huc.getResponseCode();
    if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new IOException("open HTTP connection failed.");
int len = uc.getContentLength();
InputStream in = new BufferedInputStream(uc.getInputStream());
byte[] b;
try {
    if (len != -1) {
        b = new byte[len];
        while (len > 0) {
            int n = in.read(b, b.length - len, len);
            if (n == -1) {
                throw new IOException("unexpected EOF");
            len -= n;
    } else {
        b = new byte[8192];
        int total = 0;
        while ((len = in.read(b, total, b.length - total)) != -1) {
            total += len;
            if (total >= b.length) {
                byte[] tmp = new byte[total * 2];
                System.arraycopy(b, 0, tmp, 0, total);
                b = tmp;
        if (total != b.length) {
            byte[] tmp = new byte[total];
            System.arraycopy(b, 0, tmp, 0, total);
            b = tmp;
} finally {
    in.close();
return b;
byte[]getBytes(URL url)
get Bytes
URLConnection urlconnection = url.openConnection();
if (urlconnection instanceof HttpURLConnection) {
    if (((HttpURLConnection) urlconnection).getResponseCode() >= 400)
        throw new IOException("open HTTP connection failed.");
int i = urlconnection.getContentLength();
BufferedInputStream bufferedinputstream = new BufferedInputStream(urlconnection.getInputStream());
byte abyte0[];
...
BooleangetFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)
get File Content From Web
if ((srcUrl != null)) {
    HttpURLConnection conn = (HttpURLConnection) srcUrl.openConnection();
    conn.setRequestMethod("GET");
    conn.connect();
    int responseCode = conn.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        byte tmp_buffer[] = new byte[4096];
        InputStream is = conn.getInputStream();
...
longgetFileSize(String sURL)
get File Size
int nFileLength = -1;
try {
    URL url = new URL(sURL);
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    int responseCode = httpConnection.getResponseCode();
    if (responseCode >= 400) {
        System.err.println("Error Code : " + responseCode);
        return -2; 
...
intgetFileSize(URL url)
Returns the size of the file located at the given URL.
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("HEAD");
    conn.getInputStream();
    return conn.getContentLength();
} catch (IOException e) {
    return -1;
...
intgetFilesizeFromUrl(String urlString)
get Filesize From Url
URL url = new URL(urlString.replace(" ", "%20"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    return conn.getContentLength();
} else {
...
StringgetFromURL(String URL)
get From URL
URL url = new URL(URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
int code = conn.getResponseCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
...
StringgetHTML(String pageURL, String encoding)
get HTML
StringBuilder pageHTML = new StringBuilder();
try {
    URL url = new URL(pageURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agent", "MSIE 7.0");
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
    String line = null;
    while ((line = br.readLine()) != null) {
...
StringgetHtml(String url)
get Html
StringBuilder result = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
    result.append(line);
reader.close();
return result.toString();