Access to the Proxy Server

The applet below takes a URL and Query string as input, sends this information to the proxy server Websnoop.class which is called from the shell script snoop on gauss.cs.uc.edu, and displays, in a text area of a frame, the results transmitted back from the proxy server. This applet is compiled to file K.class which resides in the same directory as snoop.html. It is called from snoop.html.

import java.net.*;
import java.io.*;
import java.awt.*;

public class K extends java.applet.Applet
{
   TextArea text, r;
   static TextArea err, out;
   Button but;

   public void init ()
   {
      setLayout(new BorderLayout());
      Panel q = new Panel();
      q.add(out = new TextArea(30,80));
      add("Center", q);
      Panel p = new Panel();
      p.setLayout(new GridLayout(1,2));
      p.add(text = new TextArea(1,60));
      Panel r = new Panel();
      r.setLayout(new GridLayout(1,2));
      r.add(but = new Button("Get it!"));
      r.add(err = new TextArea(1,30));
      p.add(r);
      add("South", p);
   }
   
   public static void getIt(String a)
   {
      URL s;

      try
      {
         s = new URL(a);

         DataInputStream in = new DataInputStream(s.openStream());

         String str = in.readLine();
         out.appendText(str+"\n");
         while (str != null)
         {
            str = in.readLine();
            out.appendText(str+"\n");
         }
      }
      catch(IOException ex)
      {
         err.appendText("Error: "+ex+"\n");
      }
   }

   public String urlencode(String str)
   {
      StringBuffer out = new StringBuffer(str.length());
      for (int i=0 ; i < str.length() ; i++)
      { 
         char ch = str.charAt(i);
         switch (ch) {
         case '?': out.append("%3F"); break;
         case '=': out.append("%3D"); break;
         case '&': out.append("%26"); break;
         default:  out.append(ch); break;
	 }
      }
      return new String(out);
   }

   public boolean action(Event evt, Object obj)
   {
      String server = new String("http://gauss.cs.uc.edu/cgi-bin/snoopy?");
      if (evt.target == but) getIt(server+urlencode(text.getText()));
      return super.action(evt, obj);
   }

   public void run()
   {
      repaint();
   }
}