Solved

Interacting with a web page

Posted on 2008-10-28
6
321 Views
Last Modified: 2012-08-13
Hi,

So I have a java applet, I want it to open up a URL, which has a text input box. I want to post some text into it, "hit" a button, then parse the output page. Here is an example:

http://www.pandorabots.com/pandora/talk?botid=a841d6e81e36b78f

basically I want my applet to paste a chat message in there, submit the form, then parse the output to get the response from the chat bot. How would I go about doing this?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:zmo
ID: 22823043
have a look at : http://www.devx.com/Java/Article/17679/0/page/3

where they suggest a solution and an API to do so... never tried it though
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 22823805
Ok that article looks promising, but how do I know the POST request to construct? I basically want to act as if I'm submitting a string from that edit field when the user hits the "Say" button.
0
 
LVL 5

Accepted Solution

by:
zmo earned 500 total points
ID: 22824298
well, you take the html source and all you'll have to set up is :

botcust2=974e6beb1e3b14df
message=<your text string>

it's equivalent to the "?botcust2=974e6beb1e3b14df&message=my%20message" you find in GET methods. The destination page is the same page.
[...]
<form method="POST" name="f">
<input type="HIDDEN" name="botcust2" value="974e6beb1e3b14df">
<table border="0" cellspacing="0">
<tr><td>
<input type="TEXT" autocomplete="off" name="message" size=60>
</td><td>&nbsp;</td><td>
<input type="SUBMIT" value="Say">
</td></tr><tr><td align="right" valign="top"><font size="-1">
<i>Powered by <a href="http://www.pandorabots.com" target="_blank">Pandorabots</a>.</i></font></td></tr></table>
</form>
[...]

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:DJ_AM_Juicebox
ID: 22824717
Ok so those would be the parameters to the request right? In that article I guess it would go something like:

    ClientHttpRequest h = new ClientHttpRequest();
    h.setParameter("botcust2", "974e6beb1e3b14df");
    h.setParameter("message",  "hi there how are you?");

But that object wants a URL in the constructor, I'm not sure what to use - for instance, when I'm looking at the page through a browser, it has this URL:

    http://www.pandorabots.com/pandora/talk?botid=a841d6e81e36b78f

is that what I use?

Finally, it should return an InputStream, after calling post(). How would I get the value as a String? Something like:

    InputStream is = h.post();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));


Thanks
0
 
LVL 5

Expert Comment

by:zmo
ID: 22824836
about the URL I guess it is what you said...
But there's only one way to know for sure, it's to try.

about the reading of the page, what you say seems logical to me.
I never could memorize all the Reader mess in java, so do like I'd do : read the reference doc ;)
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 22825133
Ok now that I see what's going on, this works perfectly:

    http://www.exampledepot.com/egs/java.net/Post.html

Thanks for helping me through it. Yeah I can never memorize the java IO classes.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 58
servlet doXXX methods 3 49
Which non-HTML GUI front end to use with Java? 3 22
Adding multiple JVM environments to RedHat 6 7 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

770 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