File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Mimic Browser Httprequests & posts" Watch "Mimic Browser Httprequests & posts" New topic
Author

Mimic Browser Httprequests & posts

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
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

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:
http://jakarta.apache.org/commons/httpclient/


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

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.
----------------------------------------
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Mar 07, 2003
Posts: 11
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

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
Greenhorn

Joined: Mar 07, 2003
Posts: 11
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

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 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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mimic Browser Httprequests & posts
 
Similar Threads
One servlet, many platforms
How does Apache Tomcat server handle the HTTPrequests?
Packaging Web App for read version CD
Managing HTTPRequests
Stuck with a Struts app and I dont know Struts - security exception problem