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

StringgetHtml(String urlString)
get Html
if (urlString.contains("id")) {
    StringBuilder html = new StringBuilder();
    InputStreamReader isr = null;
    BufferedReader br = null;
    try {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        isr = new InputStreamReader(conn.getInputStream());
...
StringgetHTML(String urlToRead)
get HTML
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
    url = new URL(urlToRead);
    conn = (HttpURLConnection) url.openConnection();
...
StringgetHTML(String urlToRead)
Makes a get request to the provided url.
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
StringBuilder result = new StringBuilder();
try {
    url = new URL(urlToRead);
    conn = (HttpURLConnection) url.openConnection();
...
StringgetHttpResponse(HttpURLConnection conn, int expectedReturnCode)
get Http Response
System.out.println("response code:" + conn.getResponseMessage());
if (conn.getResponseCode() != expectedReturnCode) {
    System.out.println("Custom Error:" + conn.getResponseCode());
    throw new IOException(conn.getResponseMessage());
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
...
StringgetHTTPResponse(String url)
get HTTP Response
HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
        "gzip".equals(con.getContentEncoding()) ? new GZIPInputStream(con.getInputStream())
                : con.getInputStream()));
StringBuilder response = new StringBuilder();
String l;
while ((l = reader.readLine()) != null) {
    response.append(l);
...
StringgetHttpResponse(String urlStr)
get Http Response
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder outputBuilder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
    outputBuilder.append(output);
conn.disconnect();
return outputBuilder.toString();
longgetLastModified(HttpURLConnection conn)
get Last Modified
long result = conn.getHeaderFieldDate("Last-Modified", 0L);
return result;
longgetLastModified(HttpURLConnection connection)
get Last Modified
return connection.getHeaderFieldDate(LAST_MODIFIED, -1);
longgetRemoteFileLength(String url)
get Remote File Length
try {
    HttpURLConnection connection;
    connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setConnectTimeout(3000);
    connection.connect();
    long length = connection.getContentLength();
    connection.disconnect();
    return length;
...
intgetRemoteFileSize(URL url)
get Remote File Size
URLConnection connection;
while (true) {
    connection = url.openConnection();
    if (connection instanceof HttpURLConnection) {
        int responseCode = ((HttpURLConnection) connection).getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            break;
        } else {
...