• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mimic Browser Httprequests & posts

 
jeroen dijkmeijer
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
Tia.
Jeroen.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://jakarta.apache.org/commons/httpclient/
 
richard ladson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
----------------------------------------
import java.net.*;
import java.io.*;
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");
toServer.flush();

for(String str = null; (str = fromServer.readLine()) != null
{
System.out.println(str);
}
toServer.close();
fromServer.close();
s.close();
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ben says - HttpClient is your friend for all of this sort of stuff.
It supports all of the emulation of the client side.
Bill
 
richard ladson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I plan to experiment with HttpClient. I downloaded ClientApp.java 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 http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/

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

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peek at Selenium, too. It's a neat HTML table driven acceptance or regression testing tool that might do some of this for you.
 
richard ladson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile Clientapp.java which has the following import:
import org.apache.commons.httpclient.HttpClient;

I downloaded the following jar file which contains the ClientHttp.class:
commons-httpclient-3.0-beta1.jar

However, when I compile the ClientApp.java I get the following message:
ClientApp.java:53: 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, ):
commons-httpclient-3.0-beta1
ClientApp.java
compileClientApp.bat

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

The import within ClientApp.java 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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ClientApp.java 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic