Babyzilla
--#--
We won't get fancy here. Let's just make something to eliminate some
of the typing we've had to do so far. Elsewhere on these pages I
show you how to use Perl, and frequently the LWP library, for HTTP
requests.
For this job, though, we're going to use Java because it's actually easier to
do it that way. Also, LWP and the Java net package libraries disguise different
parts of their operations. In this case, using Java leaves the interesting nuts and
bolts of what we're doing more exposed.
Java's net package abstracts all the socket stuff to the point where making a
connection is almost as easy as reading and writing to a file. In fact, Java's
net package has a URLConnection class that all but performs your POST for you.
We won't use it here. Instead, we'll open a raw socket to make things more
like the telnet hacking we did before. The main twist is we'll need to
create a PrintWriter from Java's OutputStreamWriter class to do our typing,
and open an InputStream for the HTML to gush back to.
import java.io.*;
import java.net.*;
...
Socket socket = new Socket("people.yahoo.com", 80);
InputStream response = socket.getInputStream();
PrintWriter agent =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
agent.println("POST /py/psAdvSearch.pv HTTP/1.0");
agent.println("Content-type: application/x-www-form-urlencoded");
agent.println("Content-length: 69";
agent.println(); // the empty line that separates head and body
agent.println(
"query=java&form=search&resultdocs=30&srcmags=checked&srcbooks=checked");
agent.flush();
This is the bare heart of Sneaky Pete, the agent that's going to go around
the 'Net hammering sites for us. We'll flesh him out a bit in a moment, but
first notice how the code directly implements the telnet sessions we used
before.