Solved

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

Posted on 2001-08-23
7
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

691 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