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

StringreadUrl(String urlString)
reads content from URL by making
StringBuffer result = new StringBuffer();
BufferedReader br = null;
InputStream inputStream = null;
try {
    URL url = new URL(urlString);
    URLConnection urlConnection = url.openConnection();
    inputStream = urlConnection.getInputStream();
    br = new BufferedReader(new InputStreamReader(inputStream));
...
byte[]readURL(URL url)
read URL
URLConnection urlconnection = url.openConnection();
InputStream inputstream = urlconnection.getInputStream();
int i = urlconnection.getContentLength();
if (i <= 0)
    i = inputstream.available();
if (i <= 0)
    return null;
byte abyte0[] = new byte[i];
...
StringreadUrl(URL url)
Read the contents of a URL as text
StringBuilder sb = new StringBuilder();
URLConnection con = url.openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(2000);
InputStream in = con.getInputStream();
String enc = con.getHeaderField("Content-Type");
enc = enc.substring(enc.indexOf("charset=") > 0 ? enc.indexOf("charset=") + 8 : enc.length());
if (enc.length() < 3)
...
StringreadUrl(URL url, String[]... headers)
read Url
BufferedReader reader = null;
StringBuilder buffer = new StringBuilder();
try {
    URLConnection connection = url.openConnection();
    for (String[] header : headers)
        connection.addRequestProperty(header[0], header[1]);
    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    int read;
...
StringreadURL(URLConnection connection, String charset)
read URL
StringBuilder builder = new StringBuilder();
InputStream input = connection.getInputStream();
byte[] array = new byte[1024];
int read;
while ((read = input.read(array)) > 0) {
    builder.append(new String(array, 0, read, charset));
return builder.toString();
...
StringreadUrlContent(URLConnection connection)
Whole HTTP response as String from given URLConnection
StringBuilder result = new StringBuilder();
try {
    Reader reader = new InputStreamReader(connection.getInputStream());
    char[] buffer = new char[50];
    int nrOfChars;
    while ((nrOfChars = reader.read(buffer)) != -1) {
        result.append(buffer, 0, nrOfChars);
} catch (IOException e) {
    throw new RuntimeException(e);
return result.toString();
StringreadURLToString(String url)
read URL To String
StringBuilder response = null;
URL urll = new URL(url);
URLConnection connection = urll.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64)");
connection.setConnectTimeout(5000);
BufferedReader in;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
response = new StringBuilder();
...
StringreadUrlToString(String urlString)
read Url To String
try {
    URL website = new URL(urlString);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder response = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        response.append(inputLine);
...
StringreadVersion(URLConnection connection)
read Version
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
BufferedReader stream = new BufferedReader(reader);
String line = stream.readLine();
stream.close();
return line;
Stringretrieve(URL url)
retrieve
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStream is = conn.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int bytesRead = 0; (bytesRead = is.read(buffer)) != -1;) {
    output.write(buffer, 0, bytesRead);
return output.toString();