Digital Stronghold


November 8, 2009

HTTP reader in Java

Filed under: Blog Post

While there are numerous ways of accessing web services, here is a primitive way of doing it. This is a simple class for reading HTTP responses. You can take advantage of this, classic examples are,

1. Reading twitter feeds for executing commands in a target machine
2. Getting the latest articles in a particular website
3. Translating through Google Translate
4. Arithmetic operations through Google
5. Currency conversion through Google

and more!

Here is the class.

/**
 * Hyper-Text Transfer Protocol Reader.
 *
 * @author joset
 */
public final class HttpReader {
	
    private Map<String, List<String>> responseHeader;
    private URL responseUrl;
    private String mimeType;
    private String charset;
    private Object content;
    private int responseCode = -1;
	
    /**
     * Constructor requiring the URL string.
     */
    public HttpReader(final String urlString)
            throws MalformedURLException, IOException {
        // open a connection.
        final URL url = new URL(urlString);
        final URLConnection urlConnection = url.openConnection();
        if (!(urlConnection instanceof HttpURLConnection)) {
            throw new IllegalArgumentException(
                    "URL protocol must be HTTP.");
        }
        final HttpURLConnection connection =
                (HttpURLConnection) urlConnection;
	
        // set up a request.
        connection.setConnectTimeout(10000);    // 10 sec
        connection.setReadTimeout(10000);       // 10 sec
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("user-agent", "spider");
	
        // send the request.
        connection.connect();
	
        // get the response.
        responseHeader = connection.getHeaderFields();
        responseCode = connection.getResponseCode();
        responseUrl = connection.getURL();
        final int length = connection.getContentLength();
        final String type = connection.getContentType();
        if (type != null) {
            final String[] parts = type.split(";");
            mimeType = parts[0].trim();
            for (int i = 1; i < parts.length && charset == null; i++) {
                final String t = parts[i].trim();
                final int index = t.toLowerCase().indexOf("charset=");
                if (index != -1) {
                    charset = t.substring(index + 8 );
                }
            }
        }
	
        // get the content.
        final InputStream stream = connection.getErrorStream();
        if (stream != null) {
            content = readStream(length, stream);
        } else if ((content = connection.getContent()) != null &&
                content instanceof InputStream) {
            content = readStream(length, (InputStream) content);
        }
	
        // close connection.
        connection.disconnect();
    }
	
    /**
     * Read stream bytes and transcode.
     */
    private Object readStream(final int length, final InputStream stream)
            throws IOException {
        final int buflen = Math.max(1024, Math.max(length, stream.available()));
        byte[] buf = new byte[buflen];
        byte[] bytes = null;
	
        for (int nRead = stream.read(buf); nRead != -1; nRead = stream.read(buf)) {
            if (bytes == null) {
                bytes = buf;
                buf = new byte[buflen];
                continue;
            }
            final byte[] newBytes = new byte[bytes.length + nRead];
            System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
            System.arraycopy(buf, 0, newBytes, bytes.length, nRead);
            bytes = newBytes;
        }
	
        if (charset == null) {
            return bytes;
        }
        try {
            return new String(bytes, charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return bytes;
    }
	
    /**
     * Get the content.
     */
    public Object getContent() {
        return content;
    }
	
    /**
     * Get the response code.
     */
    public int getResponseCode() {
        return responseCode;
    }
	
    /**
     * Get the response header.
     */
    public Map<String, List<String>> getHeaderFields() {
        return responseHeader;
    }
	
    /**
     * Get the URL of the received page.
     */
    public URL getUrl() {
        return responseUrl;
    }
	
    /**
     * Get the MIME type.
     */
    public String getMimeType() {
        return mimeType;
    }
}

Enjoy!

The young do not know enough to be prudent, and therefore they attempt the impossible — and achieve it, generation after generation. - Pearl S. Buck

Comments »

The URI to TrackBack this entry is: http://eradicus.blogsome.com/2009/11/08/http-reader-in-java/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator