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.

Repair Windows 7 System Files

8 out of 10 average PC users have their box’s system files altered by malwares, viruses, etc. We usually reinstall the OS if the antivirus and anti malware software did not perform their job well. Here’s one way to fix the corrupted system files without the need of restarting your Windows 7 box. 1. Run the Command Prompt as Administrator 2. Type the following command C:\Windows\system32\> sfc /scannow 3. After the verification phase, you will receive a message about your system files’ integrity Windows Resource Protection did not find any integrity violations.

Android Studio:Unknown Host Error

After installing Android Studio, I got the following error: Unknown host 'services.gradle.org'. Please ensure the host name is correct. If you are behind an HTTP proxy, please configure the proxy settings either in Android Studio or Gradle. Consult IDE log for more details (Help | Show Log) Solution File --> Settings --> HTTP Proxy --> Auto-detect proxy settings