Solved

Interacting with a web page

Posted on 2008-10-28
6
328 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
How to execute a Python program and gather return output in Java 2 40
Tagging and Merging on Branch 1 29
passing enum to a method 3 17
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.

809 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