Skip to main content

HTML Form Response?

1 reply [Last post]
fga
Offline
Joined: 2007-05-19

Hi everyone,
I am attempting to use SwingX-WS to retrieve and then submit an HTML form.
I have been using the "swingx-ws-2006_10_03" release, because I receive the following runtime error using the latest version (swingx-ws-2007_05_13) :

java.lang.NullPointerException
at WebSwing.main(WebSwing.java:31)

The error above occurs because I attempt to set the input values of a NULL Form, hence the Forms.getFormByName(r, "skylightsForm") invocation failed to retrieve a Form. Also, org.jdesktop.html.form.Form does not have a submit(Session s) method anymore.

Nevertheless, when using version "swingx-ws-2006_10_03", the above invocation of getFormByName(...) successfully returns a Form object, which I can then set the input values of successfully. I can also use the submit method to submit the form. However, the behavior is somewhat unexpected. Rather than return flight info for the given input values in the Response, the response contains a trimmed down version of the form.

When I change page=SEARCH to page=SELECT, then the contents of the response changes, but still does not contain the flight information as expected.

Below is the code that I using to perform the request.

Does anyone have any ides why the response keeps returning the form page, instead the appropriate flight info page? Does it have anything to do with the fact that the page is using CGI scripts?

Thanks.

----------------
Session s = new Session();
Response r = s.get("http://www.virginblue.com.au");
Form form = Forms.getFormByName(r, "skylightsForm");

//set form values
form.getInput("from1").setValue("MEL");
form.getInput("to1").setValue("SYD");
form.getInput("departDay1").setValue("20");
form.getInput("departMonth1").setValue("200706");
form.getInput("departDay2").setValue("21");
form.getInput("departMonth2").setValue("200707"); form.getInput("ADULT").setValue("1"); form.getInput("CHILD").setValue("0");
form.getInput("INFANT").setValue("0");
form.getInput("travel").setValue("2");
form.getInput("toCity1").setValue("SYD");
form.getInput("toCity2").setValue("MEL");
form.getInput("departDate1").setValue("20070620");
form.getInput("departDate2").setValue("20070721");
form.getInput("from2").setValue("SYD");
form.getInput("to2").setValue("MEL");
//form.getInput("page").setValue("SEARCH");
//form.getInput("page").setValue("SELECT");

//Submit form
r = form.submit(s);

System.out.println(r.getBody);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rbair
Offline
Joined: 2003-07-08

Hello,

> I have been using the "swingx-ws-2006_10_03" release,
> because I receive the following runtime error using
> the latest version (swingx-ws-2007_05_13) :
>
> java.lang.NullPointerException
> at WebSwing.main(WebSwing.java:31)
> ror above occurs because I attempt to set the input
> values of a NULL Form, hence the
> Forms.getFormByName(r, "skylightsForm") invocation
> failed to retrieve a Form. Also,
> org.jdesktop.html.form.Form does not have a
> submit(Session s) method anymore.

I just submitted a fix for this regression, you should see it appear in the next weekly build. The problem is that the SimpleDocumentBuilder was changed (bad!) to be namespace aware by default. I've reverted this to it's default behavior -- ie, not namespace aware, and added an alternate constructor (along with alternate static methods) allowing you to specify whether it should be namespace aware or not.

Also, instead of form.submit(), call Forms.submit(form, session).

> Nevertheless, when using version
> "swingx-ws-2006_10_03", the above invocation of
> getFormByName(...) successfully returns a Form
> object, which I can then set the input values of
> successfully. I can also use the submit method to
> submit the form. However, the behavior is somewhat
> unexpected. Rather than return flight info for the
> given input values in the Response, the response
> contains a trimmed down version of the form.

This is where screen-scraping get's to be fun. You need to check the http headers and payload going between the browser and the site and confirm that it looks the same when coming from your application. It could be that the server has several redirects in there, including cookies, which are not be handled by the application, and thus the server ends up returning a different page than what you expect.

It may also be that the page is expecting multi-part mime (which it doesn't appear to be expecting, but I haven't looked in that much detail). Multi-part mime is not currently implemented, iirc.

Let me know how it goes.

Richard B.