This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Mimic Browser Httprequests & posts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Mimic Browser Httprequests & posts" Watch "Mimic Browser Httprequests & posts" New topic

Mimic Browser Httprequests & posts

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 132

Currently i'm into the process of system testing some web jsf applications, which have been unit tested of course, but still some errors @ the web site. This web app is a bit "wizard like" and as we're approaching the end of the wizard, it takes longer and longer to get to our bugs. Now I wonder is there an easy way to mimic the http requests in some app. What I'm thinking of is: Can I fetch or serialize the httprequests fired to the servlet, store them on file. And later have some (java) app read out this file, deserialize these requests and fire the requests back to the browser?
Any help, directions or hints would be much be appreciated.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

A packet sniffer (I use tcpflow but there are others out there for free, Ethereal comes to mind) would allow you to capture the request/post data.

It wouldn't be very difficult to put together a java client to pass the post up.
Look at:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
richard ladson

Joined: Mar 07, 2003
Posts: 11
Here is some code that I wrote to simulate a simple HTTP client. Use at your own risk, as you can see I am a "greenhorn" at this.

The program reads a http body from a file and sends it to a server (localhost:80) as a GET request.
public class MyClient {

public static void main(String[] args) throws IOException
String hostname = "localhost";
int port = 80;
String filename = "/fakeit.html";
Socket s = new Socket(hostname, port);

InputStream sin = s.getInputStream();
BufferedReader fromServer = new BufferedReader(new InputStreamReader(sin));
OutputStream sout = s.getOutputStream();
PrintWriter toServer = new PrintWriter(new OutputStreamWriter(sout));

toServer.print("GET " + filename + " HTTP/1.0\n\n");

for(String str = null; (str = fromServer.readLine()) != null
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Like Ben says - HttpClient is your friend for all of this sort of stuff.
It supports all of the emulation of the client side.
richard ladson

Joined: Mar 07, 2003
Posts: 11
I plan to experiment with HttpClient. I downloaded and compiled it out-of-the-box, with no advanced thinking (eg. setting classpath, downloading dependent jars etc). The client app contains 24 imports, 3 are missing from my classpath. They are probably in the jar containing HttpClient itself.

I did a Google search on "HttpClient" and was directed to

Now what? It is not obvious how to download the appropriate jar.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Peek at Selenium, too. It's a neat HTML table driven acceptance or regression testing tool that might do some of this for you.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
richard ladson

Joined: Mar 07, 2003
Posts: 11
I am trying to compile which has the following import:
import org.apache.commons.httpclient.HttpClient;

I downloaded the following jar file which contains the ClientHttp.class:

However, when I compile the I get the following message: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.HttpClient;

This tells me that the classpath is not set properly. The following files are in a folder called c:\tmp3 (Windows/XP Pro, ):

The contents of compileClientApp.bat are:
set CLASSPATH=c:\tmp3\commons-httpclient-3.0-beta1.jar

The import within looks like this:
import org.apache.commons.httpclient.HttpClient;

The jar file contains the following:
Name: HttpClient.class
Path: org\apache\commons\httpclient\

Given all this, why dosn't javac find the HttpClient.class for the import?
richard ladson

Joined: Mar 07, 2003
Posts: 11
Never mind, I found the problem. When downloading a jar file Windows creates a folder with the same name as the jar file. I confused the folder with the jar file.

I got to work after painfully identifying the dependent classes for compilation and execution.

The ClientApp is pretty neat.

The compile dependencies are:
1) org.apache.commons.httpclient.HttpClient.class in commons-httpclient-3.0-beta1.jar
2) org.apache.commons.httpclient.MultiTheadHttpConnectionManager.class in commons-httpclient-3.0-beta1.jar
3) org.apache.commons.httpclient.methods.GetMethod.class in commons-httpclient-3.0-beta1.jar

The execution dependencies are:
1) org.apache.commons.logging.LogFactory.class in commons-logging.jar
2) org/apache/commons/codec/DecoderException.class in commons-codec-1.3.jar

I wish I knew a good way to find a jar that contains a given class!
I agree. Here's the link:
subject: Mimic Browser Httprequests & posts
It's not a secret anymore!