~ Essays ~
         to essays    essays
   
~ Bots Lab ~
         to bots    bots lab
(Courtesy of fravia's searching lores)

(¯`·.¸ Java Bots introduction ¸.·´¯)

by Dolmen
(very slightly edited by fravia+)
published at searchlores in Mai 2001

Java could indeed be a simple way to develop more portable scrolls :-)

Java Bots introduction

In this introduction I'll show you how to write a single bot in Java. Of course, when I'm talking about Java, I'm not thinking to applets but to applications.

Why is Java better than Perl or other languages at writing bots ?

Now, here is a simple bot that downloads a page from the URL given on the command line and outputs it preceded by the HTTP headers (if it is an http:// URL).


Java QuickBot: http://www.searchlores.org/QuickBot.java
QuickBot class: http://www.searchlores.org/QuickBot.class

/*
 * To compile (you need a JDK from http://java.sun.com/):
 *   javac QuickBot.java
 * To run with a Sun VM:
 *   java QuickBot http://www.searchlores.org/
 * through an HTTP proxy:
 *   java -Dhttp.proxySet=true -Dhttp.proxyHost=my.proxy.com -Dhttp.proxyPort=8080 QuickBot http://www.searchlores.org/
 * or with a Microsoft VM (bundled with IE):
 *   jview QuickBot http://www.searchlores.org/
 */

import java.net.*;
import java.io.BufferedInputStream;

public class QuickBot {
	public static void main(String[] args) {
		try {
			URLConnection cnn = new URL(args[0]).openConnection();
			cnn.setDoOutput(false);
			cnn.setDoInput(true);

			cnn.connect();

			int i = 1;
			String headerFieldKey;
			while ((headerFieldKey = cnn.getHeaderFieldKey(i)) != null) {
				System.out.println(headerFieldKey+": "+cnn.getHeaderField(i));
				i++;
			}
			if (i > 1)
				System.out.println();

			BufferedInputStream in = new BufferedInputStream(cnn.getInputStream());
			int b;
			while ((b = in.read()) != -1)
				System.out.write(b);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
}

I'll show you in a next essay how to write your bot as an HTTP servlet to show you that you can do the same as some PHP or Perl CGI scripts seen on this site, but cleaner.

Dolmen (dolmen*at*bigfoot*dot*com)
May 1st, 2001


         to essays    Back to essays
   
         to bots    Back to bots lab
(c) III Millennium: [fravia+], all rights reserved