Solved

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

Posted on 2001-08-23
7
170 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:mazer-x26
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:sdussinger
ID: 6418092
Listening...
0
 

Author Comment

by:mazer-x26
ID: 6418465
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
0
 
LVL 4

Accepted Solution

by:
sdussinger earned 200 total points
ID: 6419155
1) The code above appears to be doing an HTTP GET. To do a POST, you need to add connection.setMethod ("POST") to the code where you do the setDoOutput ().

2) The parameters seem to be encoded ok.

3) I'm not sure what you want to do here.

4) I don't know any esay way that you can set a timeout on the connection. You can set SO_TIMEOUT on a socket, but since there is no way that I know of to get the Socket from the URLConnection, I don't think this'll work. There have been some threads on DejaNews which discuss this, but the basic gist is that you need to use a separate thread to implement the timeout.

--Steve
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

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

--Steve
0
 

Author Comment

by:mazer-x26
ID: 6419788
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());
     }
0
 

Author Comment

by:mazer-x26
ID: 6419794
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());
     }
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6420278
Glad I could help.

--Steve
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question