Skip to main content

HTTP reader in Java

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

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Moving on

So I bought my self a 2.5″ hard drive enclosure for about 750 bucks. I wish I have my own tools for constructing a device as such. Instead of going for a 2GB USB flash drive, I chose to salvage the 30GB hard drive of an incapacitated laptop. Cool isn’t it? Though relatively slower than a typical USB flash drive, the storage capacity outweighs it. [To God] Thanks for being there always.

Bin to hex converter in 16-bit DOS assembly

Just a quick code like putting your keyboard where your brain is. There should be another way of doing this. I hate this code. start: xor al, al xor bl, bl xor cl, cl mov dl, 4 ; use dl as counter input: mov ah, 00 int 16h cmp ah, 1ch je exit cmp al, '0' jb input cmp al, '1' ja input convert: mov cl, dl dec cl ; dl - 1 sub al, 30h ; get original value shl al, cl ; place in the appropriate bit position or bl, al ; save in bl dec dl ; prepare for the next bit jnz input ; must be 4 bits cmp bl, 9 ja letter jbe number letter: add bl, 37h ; because 'A' - 37h is 0Ah jmp here number: add bl, 30h ; because '0' - 30h is 00h here: call display jmp start display: mov ah, 02h ...