It's not a secret anymore!
The moose likes Servlets and the fly likes Simulating browser in a application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Simulating browser in a application" Watch "Simulating browser in a application" New topic

Simulating browser in a application

san jan

Joined: Sep 26, 2000
Posts: 24
may i know how to simulate a browser in an application.
i wanna send details of a form to a url on internet & wanna catch the response & give status whether the submit was perfect or not.
i wanna find whether this can be done by URLConnection? can we submit a form through URLConnection?
its simulating a browser functionality in the application.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Here is the almost complete code for POSTING data to a HTTP resource, you can fill in the exception handling to suit.

san jan

Joined: Sep 26, 2000
Posts: 24
hi james,
thanks a lot, but am behind a firewall/proxy. i am getting following exception. how to set for a proxy address,port, and also provide username and password, so that application makes the connection.
the following exception i am getting at "httpConnection.connect()" line.
I have tried it but in vain.
can i have ur e-mail id?

[This message has been edited by santosh kumar janmanchi (edited August 04, 2001).]
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
If you are behind a firewall, you need to define some system properties that point to your proxy:
System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxySet", "true");
Note that this works for URLConnection access, but not plain sockets.
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
san jan

Joined: Sep 26, 2000
Posts: 24
Hi Phil,
thank u. now i am able to connect to internet sites. i am able to read sites info.
am giving proxy settings, am giving authentication info thru subclassing Authenticator & then using its static method setDefault().
site i tested is :
from this site's source i took the form that searches the site for greeting cards particular to some given words(can see under "Cool Services").
The code snippet i used for posting is

<B>I have tried all the above 3 options to post to the site, the response i get is '200', that is ok.
But the response page should contain 'searched word:birthday' as i have given birthday as the word to search, but instead gives in response page as 'searched word:' i.e., null is taken.</B>
not only with this, but with many other sites also am not getting the page am supposed to get, infact the post is not happening in a proper manner.
san jan

Joined: Sep 26, 2000
Posts: 24
infact the code works perfectly fine with the following site
which is given in sun's tutorials for testing of posting data to a url using url-connection objects.
but the same program is not posting data to any other sites, its not working perfectly.
I agree. Here's the link:
subject: Simulating browser in a application
jQuery in Action, 3rd edition