Solved

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

Posted on 2001-08-23
7
168 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
wordmultiple challenge 12 90
Java array passed to SQL where clause 23 43
compre toata in where clue oracle 4 44
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now