Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applets reading web pages

 
paul wheaton
Trailboss
Pie
Posts: 21894
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want an Applet to generate a POST command to a URL. There I have a servlet that will give back an HTML page. I want the applet to read and use the HTML page. How do I go about doing this?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Applets are (usually) restricted to connecting to the host they were loaded from. I'll assume this is the case, but if not, you'll need some sort of gateway or proxy running on the server which served the applet.
2. Open a connection to the server, either by using a Socket directly, or opening an OutputStream using URL.getConnection().getOutputStream().
3. Send the post request as per the HTTP spec it's easy, just a sequence of lines. the first line indicates the directive:
<pre>
POST /cgi-bin/something.cgi HTTP/1.0
</pre>
The next few lines are any number of standard HTTP headers, for example:
<pre>
Referer: file:/myapplet/or/whatever
User-Agent: Some Name or pretend to be a browser
Accept */*
</pre>
Then the important lines, the format and length:
<pre>
Content-type: application/x-www-form-urlencoded
Content-length: 123
</pre>
Obviously the length should be the length of whatever you are sending. I typically build it into a StringBuffer and use StringBuffer.length() for the content length.
Then add a blank line, followed by your content, as one line, url encoded eg:
<pre>
name=Paul+Wheaton&site=Electric+Porkchop
</pre>
4. Get the reply from the server, either by reading from the input same socket; reading form the stream returned by URLConnection.getInputStream(); or getting the whole thing in one go using URLConnection.getContent().
5. Close your connections, sockets etc and process whatever you got back.
For more information see:

  • Java Network Programming (O'Reilly) [full example code on pp277-278]
  • The HTTP spec, RFC 1945 (http://deesse.univ-lemans.fr:8003/Connected/RFC/1945/index.html)
  • Java Examples in a Nutshell (O'Reilly) [ch. 9]
  • Web Client Programming (O'Reilly) [pp34-38]

  • Has this helped?
    [This message has been edited by Frank Carver (edited September 30, 1999).]
 
paul wheaton
Trailboss
Pie
Posts: 21894
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this has helped a lot!
Your example does more than I found in the Java Servlet Programming book (O'Reilly). I was wonderering where all the POST stuff went, and your example has it all. I'm going to try it now.
 
paul wheaton
Trailboss
Pie
Posts: 21894
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't find "Web Client Programming" - different name?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sorry. The full title is "Web Client Programming with Perl". On the cover the "with Perl" is in quite small print though...
(ISBN:1-56592-214-X)
Despite the Perl bias, I find it useful as very few books seem to cover automating client operations. Most focus on purely server-side code, and assume the client is a human browser user.
For this purpose, my top recommendation would have to be Java Network Programming, though.
A very useful book.
 
paul wheaton
Trailboss
Pie
Posts: 21894
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a new "Java Network Programming" book out. Different author and publisher. Good reviews on amazon. Going to look into that one too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic