20-CS-694 Advanced Programming Techniques Spring 2012
Networking

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Next     ProxyClient.java     ProxyServer.java     URL Class      All lectures

Proxy Client

This is a client that will use the ProxyServer of the previous slide directly. Be sure to download and compile ProxyServer.java and place ProxyServer.class in <httpd-root>/cgi-bin or whatever your webserver's cgi root is (check the webserver's config file). Also download the script server and place it in the cgi root directory, making it executable. Also be sure to start your http server (usually apache).

A sample query is: gauss.ececs.uc.edu/franco_files/index.html

Click on the "Get It!" button to make the query - only text is displayed.

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class ProxyClient extends Applet implements ActionListener {
   JTextArea r;
   JTextField text;
   static JTextArea err, out;
   JButton but;

   public void init () {
      setLayout(new BorderLayout(10,10));
      Panel p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new Label("From URL:"));
      p.add("Center", out = new JTextArea(25,60));
      add("Center", p);
      
      p = new Panel();
      p.setLayout(new BorderLayout(10,10));
      Panel q = new Panel();
      q.setLayout(new BorderLayout());
      q.add("North", new JLabel("Enter Query Here"));
      q.add("Center", text = new JTextField());
      p.add("North", q);
      
      q = new Panel();
      q.setLayout(new BorderLayout(10,10));
      q.add("East", but = new JButton("Get it!"));
      Panel r = new Panel();
      r.setLayout(new BorderLayout());
      r.add("North", new JLabel("Messages"));
      r.add("Center", new JScrollPane(err = new JTextArea(4,30)));
      q.add("Center", r);
      p.add("Center", q);
      add("South", p);
   }
   
   public void start () {
      but.addActionListener(this);
   }
   
   public static void getIt(String a) {
      URL s;

      try {
         s = new URL(a);

         InputStream ins = s.openStream();
         BufferedReader in = new BufferedReader(new InputStreamReader(ins));

         String str = in.readLine();
         out.append(str+"\n");
         while (str != null) {
            str = in.readLine();
            out.append(str+"\n");
         }
      } catch (ConnectException ce) {
         err.append("Connection Refused\n");
      } catch(IOException ex) {
         err.append("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 void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == but) {
         String server = new String("http://localhost/cgi-bin/server?");
         getIt(server+urlencode(text.getText()));
      }
   }
}