| Author |
override value="" ???
|
Stephen Huey
Ranch Hand
Joined: Jul 15, 2003
Posts: 618
|
|
I've been trying to auto post a form on someone else's site from within my Java code, but it's an ASP page, and from the client side, all I can see in the HTML source is that the form field values are hardcoded to "", so I'm wondering if it's even possible for me to send a POST command to it. Every way I've tried seems to fail... Now, I'm so frustrated that I'm trying to narrow down my question to this simple piece of it (because it seems no one can answer my question), but if you want to know more details about my problem in general, then feel free to check this out: http://www.coderanch.com/t/279278/JSP/java/compare-year-date 070 You may think I'm asking a simple question, but no one seems to know the answer, so that's why I chose the advanced forum. I'm thinking this just isn't something that people do very often--that usually most programmers have more control of the forms they're working with.
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
Your java code will have to play the role of the Web browser, so it will have to make a http request to the URL of the form, and retrieve a stream from that URL. You will then have to parse that stream, and find the form tags. Then you can discover what the form element names are. Using this knowledge, you can create an http request and write the headers that a web browser normally would. In the body, you would include the form data - the form elements you wish to auto-fill, along with the values you wish to provide for them. There may be other complications - security for one. The servlet container running your java code may not allow you to open a network connection to a foreign URL. If the form contains client-side code that is important in validating the form or providing values, you'll have to duplicate that logic in your servlet. You may also have to keep track of client-side state information. If the foreign site is using cookies along with the form, you'll have to read and possibly write them as well - in effect, you have to take over all the duties a web browser would perform if a user was sitting down to enter the form data and clicking the "submit" button manually. It's a lot of work, but it's certainly possible to do what you are asking.
|
Rob
SCJP 1.4
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
|
|
There is in fact a handy Java toolkit for emulating a web browser. It is called HttpClient - part of the "commons" project at Jakarta/apache I was just using it to do a load test emulating POST form submission. VERY handy stuff - keeps track of cookies etc just like a browser. Bill
|
Java Resources at www.wbrogden.com
|
 |
Stephen Huey
Ranch Hand
Joined: Jul 15, 2003
Posts: 618
|
|
|
Just curious if anybody really looked at the HTML on that page. At the end of the work day I figured out what someone in another forum told me tonight--that I was posting to the wrong page...that I should have been trying to post to the page that the form on there was posting to rather than the page that the form was on. Actually, the reason it tricked me was that the form on that page was posting to itself, but the ASP must then do a redirect, b/c if you enter in the form by hand and submit it to go to a confirmation page (where the user can review the entries) and take a look at that page's source, you can see that you can submit to THAT page instead, and it will work! However, I wanted to submit it in such a way that my user got taken to the confirmation/review page, but when I submit it to that page, it just goes straight to the final confirmation page and so my users never get a chance to review the data that I auto-submitted. However, I guess it's ok--they'll never know what they're missing out on, and they don't really need it.
|
 |
 |
|
|
subject: override value="" ???
|
|
|