~ Essays ~
|
|
|
|
essays |
|
|
~ Bots Lab ~
|
|
|
|
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 ?
- Language: Java is cleaner. Few mistakes are possibles compared to C or Perl.
- "Code scalability": Java is good for small projects (see below) as well as big ones.
But it is even better at growing projects because the language and its API push you to use
separate classes, packages, inheritance...
- Portability: every Windows machine now has Internet Explorer. And IE includes a Java
virtual machine. You'll will be able to run your bots anywhere cleanly without installing
any add-ons. Even on Unix systems you now find JVMs as base packages where Perl is just
an option from the vendor (IBM AIX to name one).
- APIs: clean, and high level APIs handling HTTP connexion are available in the base package.
SSL, mail, servlets are available as free extensions from Sun.
- Threads: Java natively handles multi-threading as a core component of its API. Writing bots
accessing simultaneously at multiples sites can be done fingers in the nose.
- You can make your bot a web application with the serlet API.
- Many free extensions are available to add what the base API lacks.
- Reversing: learn from others as Java classes are so easily reversibles.
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
|
|
|
|
Back to essays |
|
|
|
|
|
|
Back to bots lab |
|
|
(c) III Millennium: [fravia+], all rights
reserved