Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Post HTML forms and reading results

Reply
Thread Tools

Post HTML forms and reading results

 
 
C. Kindle
Guest
Posts: n/a
 
      05-16-2004
is it possible for a java application to enter values in a html form, post
it, and reading the resulting html document, e.g. google.com? it seems that
the other side has to be some kind of cgi-script or it doesn't work. i found
an example at
http://java.sun.com/docs/books/tutor...ngWriting.html,
but trying it with google, or any other html form, it doesnt work.

source:
=====
public void doPost(){
String queryValue = URLEncoder.encode("java");
try {
URL url = new URL(http://www.google.com);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter(
connection.getOutputStream());
out.println("q=" + queryValue);
out.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
catch(Exception e) {
//On exception output error message
System.out.println("url connection error");
}
}


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      05-16-2004
On Sun, 16 May 2004 23:47:24 +0200, C. Kindle wrote:

> is it possible for a java application to enter values in a html form, post
> it, and reading the resulting html document, e.g. google.com?


I hear that Google and such are very fussy
about who they serve..

(They have pre-designed Java API's
for accessing search results.)

> catch(Exception e) {
> //On exception output error message


// always advisable..
e.printStackTrace();

> System.out.println("url connection error");
> }
> }


HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
 
Mike Smith
Guest
Posts: n/a
 
      05-17-2004
> but trying it with google, or any other html form, it doesnt work.

Try:
http://www.rgagnon.com/howto.html

Under Java choose Networking, at the bottom of the list of code snippits is
one called: "Fetch a page from Google". Basically, you just have to look
like a browser when you connect. It's not rocket science.

Mike


 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      05-17-2004

"C. Kindle" <> wrote in message
news:c88nou$as0$...
> is it possible for a java application to enter values in a html form, post
> it, and reading the resulting html document, e.g. google.com? it seems

that
> the other side has to be some kind of cgi-script or it doesn't work. i

found
> an example at
>

http://java.sun.com/docs/books/tutor...ngWriting.html,
> but trying it with google, or any other html form, it doesnt work.
>


you may want to take a look at the apache commons httpclient. this is a
reusable library containing much functionality related to HTTP.


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-17-2004
On Mon, 17 May 2004 05:15:05 GMT, Mike Smith wrote:

>> but trying it with google, or any other html form, it doesnt work.


(Snip URL that leads..)
<http://www.rgagnon.com/javadetails/java-0399.html>

> ...Basically, you just have to look
> like a browser when you connect. It's not rocket science.


Huh! Another great example..
Wish I had thought to hunt around the
"Real's How-To" at the time..

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-17-2004
> On Mon, 17 May 2004 05:15:05 GMT, Mike Smith wrote:

C.K. wrote *actually* (but implied attribution to Mike)
>>> but trying it with google, or any other html form, it doesnt work.


(Mike Smith *replied*..)
> (Snip URL that leads..)
> <http://www.rgagnon.com/javadetails/java-0399.html>


Oops!
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-17-2004
On Sun, 16 May 2004 23:47:24 +0200, "C. Kindle" <> wrote
or quoted :

>is it possible for a java application to enter values in a html form, post
>it, and reading the resulting html document, e.g. google.com? it seems that
>the other side has to be some kind of cgi-script or it doesn't work. i found
>an example at
>http://java.sun.com/docs/books/tutor...ngWriting.html,
>but trying it with google, or any other html form, it doesnt work.


see http://mindprod.com/jgloss/cgi.html
and http://mindprod.com/jgloss/fileio.html
for sample code.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
C. Kindle
Guest
Posts: n/a
 
      05-17-2004
thank you for your answers. by querying a page with the url of the resulting
page, search parameters and their values it works

what i want is to simulate a html form submission from java, since some
query forms require the query to be executed by their 'original' query form.
what i found doesn't seem to work.


"Mike Smith" <> schrieb im Newsbeitrag
news:tTXpc.129$.. .
> > but trying it with google, or any other html form, it doesnt work.

>
> Try:
> http://www.rgagnon.com/howto.html
>
> Under Java choose Networking, at the bottom of the list of code snippits

is
> one called: "Fetch a page from Google". Basically, you just have to look
> like a browser when you connect. It's not rocket science.
>
> Mike
>
>



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-17-2004
On Mon, 17 May 2004 09:38:43 +0200, C. Kindle wrote:

Apparently re this thread..
<http://google.com/groups?th=c8303b0abcbc08fc>
Please do not change the subject line of a thread
or strat a new thread unless it is a different subject.

Even thenit is best to find the first at Google
groups and link to it.

Further comments to bottom..

> "Mike Smith" <> schrieb im Newsbeitrag

(C.K.)
>>> but trying it with google, or any other html form, it doesnt work.

...
>> http://www.rgagnon.com/howto.html
>>
>> Under Java choose Networking, at the bottom of the list of code snippits is
>> one called: "Fetch a page from Google". Basically, you just have to look
>> like a browser when you connect. It's not rocket science.


<top-post corrected>
Please do not top-post either C., as
it breaks the thread of the ..thread.
<http://www.physci.org/codes/javafaq.jsp#netiquette>

> thank you for your answers. by querying a page with the url of the resulting
> page, search parameters and their values it works
>
> what i want is to simulate a html form submission from java, since some
> query forms require the query to be executed by their 'original' query form.
> what i found doesn't seem to work.


Now.. you may have to put a little more
effort and thought into your statements..

I have never seen an..
"DoesNotSeemToWorkException"
<http://www.physci.org/codes/javafaq.jsp#exact>

Also, you geve a great little (the best kind)
example whrn you 1st posted, could you show us
_exactly_ what you have now?
<http://www.physci.org/codes/sscce.jsp>

Also, I am not sure how much experience you have
with debuggin, but it seems this may be a better
group for the moment..
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-17-2004
C. Kindle wrote:
> thank you for your answers. by querying a page with the url of the resulting
> page, search parameters and their values it works
>
> what i want is to simulate a html form submission from java, since some
> query forms require the query to be executed by their 'original' query form.
> what i found doesn't seem to work.


Unfortunately, you haven't said much that could give us a clue what
isn't working.

One guess is that in your original code sample, you specified the URL as
"http://www.google.com". That's wrong; you need to specify the URL that
the form should be submitting to, not the URL that produces the form to
begin with. In Google's case, that's "http://www.google.com/search".

If you don't know that for your specific form (and don't want to collect
it ahead of time for some reason), then you're left with the regrettable
task of making *two* HTTP requests... the first will return the form,
and you need to parse it looking for the 'action' attribute of the form
tag. After you've got that, you can then submit the real form request.

In the latter case, you might actually want to look into some unit
testing tools like Canoo WebTest, which are decent at letting you script
interaction with web sites.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading URLs with POST data vs. w/out POST Hal Vaughan Java 4 01-14-2008 12:38 AM
A question about HTML Forms and emailing the results wmburns HTML 5 02-12-2006 09:47 PM
forms authentication -- expired forms cookie vs. not provided forms cookie Eric ASP .Net Security 2 01-27-2006 10:09 PM
Post post post. Shel-hed Computer Support 2 11-08-2003 07:41 AM
ASP, FORMS, POST METHOD And Post with out form(???) Don Glover the younger ASP General 0 07-13-2003 02:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57