Link to home
Start Free TrialLog in
Avatar of mazer-x26
mazer-x26

asked on

Emulate & Intercept a Form POST from a Servlet - BONUS AVAIL

I've got an inherited servlet, and what I need to do is make a mock-up with different values for a form post. I've seen a couple packages but they are WAY over-bloated. Once the form POST has been submitted, I want to read the response back into a custom object that has the response code & response value, ie: the output of the page.

If you respond quickly to the question I will award bonus points.
Avatar of sdussinger
sdussinger

Listening...
Avatar of mazer-x26

ASKER

Let me give you the code I'm using now, maybe that will help, btw: it has to work with https.

    URL url = new URL(scriptURL);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    String username = "theuser";
    String password = "thepass";
    String moddate = "0000000000000000";
    String redirect = "http://www.url.com/intranet/home.nsf/frNewHome";
    PrintWriter out = new PrintWriter(  connection.getOutputStream());
    out.println( "?Login=" + URLEncoder.encode("true") + "%%ModDate=" + URLEncoder.encode(moddate) + "RedirectTo=" + URLEncoder.encode(redirect) + "&acctid=" + URLEncoder.encode(username) + "&passwd=" + URLEncoder.encode(password));
     out.close();
     BufferedReader in = new BufferedReader( new InputStreamReader(  connection.getInputStream()));
    String inputLine;
     while ((inputLine = in.readLine()) != null)
        output.println(inputLine);
     in.close();

Output is just a printwriter that rights to the response stream, ie: PrintWriter output = response.getWriter();

Now when I set the url to an asp page it lists these values into a list when I do an iteration over all of the elements in Request.Form, so to me it appears it works, but when I tested it against a different url I got basically a failed login, or just a re-write of the page when I printed it. I need to be sure that the above code is:

A:) doing a form POST
B:) is properly encoding the elements
C:) (optional-good bonus for this); how to detect when a redirect is sent, as when I pointed it to a page that checks the credentials, then redirected, java throws a FileNotFoundException I believe.
D:) set a timeout incase the remote server does not respond in a timely fashion, say 3 seconds.

If you know of a better (but fairly lightweight, as this has to be quick) way, then that would also be great.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of sdussinger
sdussinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry. That's setRequestMethod ("POST"). Its a member of HttpURLConnection, so you'll have to cast your URLConnection...

--Steve
If i could curse you out I would, you are the man! I'll post the final piece of code that worked in case anyone else would like to view it in the future, thanks a lot.

    try {
    String scriptURL = "http://url";
    URL url = new URL(scriptURL);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    String username = "user";
    String password = "pass";
    PrintWriter out = new PrintWriter(  connection.getOutputStream());
    out.println("Username=" + URLEncoder.encode(username) + "&Password=" + URLEncoder.encode(password));
     out.close();
     BufferedReader in = new BufferedReader( new InputStreamReader(  connection.getInputStream()));
    String inputLine;
     while ((inputLine = in.readLine()) != null)
        output.println(inputLine);
     in.close();
     } catch (Exception e) {
      output.print(e.toString());
     }
Here's the relevant comment below:

Sorry. That's setRequestMethod ("POST"). Its a member of HttpURLConnection, so you'll have to cast your
URLConnection...

final code:
    try {
    String scriptURL = "http://url.com";
    URL url = new URL(scriptURL);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    String username = "user";
    String password = "pass";
    PrintWriter out = new PrintWriter(  connection.getOutputStream());
    out.println("Username=" + URLEncoder.encode(username) + "&Password=" + URLEncoder.encode(password));
     out.close();
     BufferedReader in = new BufferedReader( new InputStreamReader(  connection.getInputStream()));
    String inputLine;
     while ((inputLine = in.readLine()) != null)
        output.println(inputLine);
     in.close();
     } catch (Exception e) {
      output.print(e.toString());
     }
Glad I could help.

--Steve