Solved

Interacting with a web page

Posted on 2008-10-28
6
303 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now