Java Utililty Methods HTTP Read

List of utility methods to do HTTP Read

Description

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

Method

byte[]getByteaArrayFromConn(HttpURLConnection conn, boolean isSuccess)
get Bytea Array From Conn
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int bytesRead = 0;
while ((bytesRead = is.read(buff, 0, buff.length)) != -1) {
    baos.write(buff, 0, bytesRead);
byte[] bytes = baos.toByteArray();
...
byte[]getContent(final HttpURLConnection con)
get Content
try {
    InputStream inputStream = con.getInputStream();
    if (inputStream != null) {
        try {
            return readStream(inputStream);
        } finally {
            inputStream.close();
} catch (IOException e) {
return null;
StringgetContent(final String urlString)
Gets the contents of a URL
try {
    final URL url = new URL(urlString);
    final URLConnection urlConnection = url.openConnection();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    String inputLine = "", content = "";
    while ((inputLine = reader.readLine()) != null)
        content += inputLine;
    reader.close();
...
StringgetContent(String urlStr)
get Content
StringBuilder sb = new StringBuilder();
InputStreamReader inputStreamReader = null;
BufferedReader br = null;
try {
    URL url = new URL(urlStr);
    URLConnection conn = url.openConnection();
    inputStreamReader = new InputStreamReader(conn.getInputStream());
    br = new BufferedReader(inputStreamReader);
...
StringgetContentAsString(URL url)
get Content As String
URLConnection conn = url.openConnection();
conn.connect();
InputStream in = conn.getInputStream();
Reader r = new InputStreamReader(in, "UTF-8");
int c;
StringBuffer buf = new StringBuffer();
while ((c = r.read()) != -1) {
    buf.append((char) c);
...
StringgetContentEncoding(URL url)
Returns the value of the content-encoding header field.
String encoding = null;
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) url.openConnection();
    encoding = conn.getContentEncoding();
} finally {
    if (conn != null) {
        conn.disconnect();
...
StringgetContentFromHttpGetBasicAuth(String urlString, String username, String password)
Will return empty string in case of errors.
try {
    URL url = new URL(urlString);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    String userpass = username + ":" + password;
    String basicAuth = "Basic " + base64encode(userpass.getBytes());
    con.setRequestProperty("Authorization", basicAuth);
    InputStream is = con.getInputStream();
...
StringgetContentFromURL(String sURL)
get Content From URL
String content = "ERROR";
URL url = null;
try {
    url = new URL(sURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    Scanner s;
    if (connection.getResponseCode() != 200) {
        s = new Scanner(connection.getErrorStream());
...
StringgetContentFromURL(String url)
get Content From URL
try {
    return getContentFromURL(new URL(url));
} catch (Exception e) {
return null;
longgetContentLength(URL url)
get Content Length
URLConnection conn = url.openConnection();
Long contentLength = conn.getContentLengthLong();
if (contentLength != null && contentLength >= 0) {
    return contentLength;
String contentLengthValue = conn.getHeaderField("content-length");
if (contentLengthValue != null) {
    return Long.valueOf(contentLengthValue);
...