HTTPClientExample.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * This is a very simple example of how to use the HTTPClient package in an
  3.  * Applet. It just POSTs a request to a cgi-script on the server when you
  4.  * hit the 'Doit' button, and then displays the returned headers and data in
  5.  * a text window.
  6.  */
  7. package HTTPClient.doc;
  8. import java.applet.Applet;
  9. import java.awt.Button;
  10. import java.awt.Graphics;
  11. import java.awt.TextArea;
  12. import java.awt.BorderLayout;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import HTTPClient.HTTPResponse;
  16. import HTTPClient.HTTPConnection;
  17. public class HTTPClientExample extends Applet implements Runnable, ActionListener
  18. {
  19.     private HTTPConnection con;
  20.     private HTTPResponse   rsp = null;
  21.     private String         script = "/cgi-bin/my_script.cgi";
  22.     private String         disp = "";
  23.     private Thread         thread = null;
  24.     private boolean        done = false;
  25.     private TextArea       text;
  26.     public void init()
  27.     {
  28. /* setup a text area and a button */
  29.         setLayout(new BorderLayout());
  30. add("Center", text = new TextArea(60, 60));
  31. text.setEditable(false);
  32. Button doit = new Button("Doit");
  33. doit.addActionListener(this);
  34. add("South", doit);
  35.  
  36. /* get an HTTPConnection */
  37.         try
  38.         {
  39.     con = new HTTPConnection(getCodeBase());
  40.         }
  41.         catch (Exception e)
  42.         {
  43.             disp = "Error creating HTTPConnection:n" + e;
  44.             repaint();
  45.             return;
  46.         }
  47.     }
  48.     public void start()
  49.     {
  50. /* run the http request in a separate thread */
  51. if (thread == null)
  52. {
  53.     done   = false;
  54.     thread = new Thread(this);
  55.     thread.start();
  56. }
  57.     }
  58.     public void run()
  59.     {
  60. try
  61. {
  62.     while (true)
  63.     {
  64. /* wait for the button to be pressed */
  65. waitForDoit();
  66. if (done)  break;
  67. /* POST something to the script */
  68. disp = "POSTing ...";
  69. repaint();
  70. rsp = con.Post(script, "Hello World again");
  71. repaint();
  72.     }
  73. }
  74. catch (Exception e)
  75. {
  76.     disp = "Error POSTing: " + e;
  77.     e.printStackTrace();
  78.     repaint();
  79. }
  80.     }
  81.     
  82.     private synchronized void waitForDoit()
  83.     {
  84. try { wait(); } catch (InterruptedException ie) { }
  85.     }
  86.     private synchronized void notifyDoit()
  87.     {
  88. notify();
  89.     }
  90.     public void stop()
  91.     {
  92. if (thread != null)
  93. {
  94.     done   = true;
  95.     notifyDoit();
  96.     thread = null;
  97. }
  98.     }
  99.     public void actionPerformed(ActionEvent evt)
  100.     {
  101. notifyDoit(); // tell request thread to do the request
  102.     }
  103.     public void paint(Graphics g)
  104.     {
  105. text.setText(disp + "n");
  106. if (rsp == null) return;
  107. try
  108. {
  109.     text.append("n---Headers:n" + rsp.toString());
  110.     text.append("n---Data:n" + rsp.getText() + "n");
  111. }
  112. catch (Exception e)
  113. {
  114.     text.append("n---Got Exception:n" + e + "n");
  115. }
  116.     }
  117. }