Navigation

Own programs

Snippets

Libraries

Deutsch

DownloadHelper

Java gives you many possibilities to connect to servers. Too bad they are not easy possibilities. Therefore I made myself a small helper class to handle the most common communications to webservers. It consists of the following methods:

More specific information about the functions and their parameters are in the Javadoc.

Example:

ArrayList answer = null;
try {
	// download a page.	page to download/no referer/default user agent/timeout (5 sec)/3 retries
	answer = klaue.DownloadHelper.Downloader.downloadPage(new URL("http://www.xkcd.com/"), null, null, 5000, 3);
} catch (Exception e) {
	e.printStackTrace();
}

if (answer == null) {
	System.out.println("some error happened");
} else {
	System.out.println("Downloaded page:");
	// print out first 6 lines
	for (int i = 0; i < 6; ++i) {
		System.out.println(answer.get(i));
	}
}

Result:

Downloaded page:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="/static/general.css" type="text/css" media="screen" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>xkcd - A webcomic of romance, sarcasm, math, and language - By Randall Munroe</title>

Download (Source & Classes): DownloadHelper.jar

Changes:
02/27/2011:
- Fixed a threading problem in the cookie handler
- Added a wait of one second between retries
- Fixed a retry-related bug in downloadPost