Solved

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

Posted on 2001-08-23
7
169 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 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

17 Experts available now in Live!

Get 1:1 Help Now