Client HTTP en Java avec interface utilisateur


Troisième déclinaison du mini client http, cette fois en version java avec un interface graphique.
Voila à quoi ça resemble :



Toujours très simple donc, il suffit de configurer son proxy et c'est parti !!

import java.util.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;



public class GClient extends JFrame
{

  public class ProxyDialog extends JDialog
  {
    JTextField hostTF, portTF;

    public ProxyDialog(Frame parent, boolean modal)
    {
      super(parent, modal);

      JPanel panel1, panel2, panel3;
      JButton okButton, cancelButton;

      Container contentPane = this.getContentPane();

      panel1 = new JPanel();
      panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
      contentPane.add(panel1, BorderLayout.SOUTH);
      okButton = new JButton("OK");
      panel1.add(okButton);
      cancelButton = new JButton("Cancel");
      panel1.add(cancelButton);

      panel2 = new JPanel();
      panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
      contentPane.add( panel2, BorderLayout.NORTH);
      panel2.add(new JLabel("Proxy ? "));
      panel2.add(useProxy);

      panel3 = new JPanel();
      panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
      contentPane.add(panel3, BorderLayout.CENTER);
      hostTF = new JTextField(proxy, 20);
      portTF = new JTextField("" + port, 5);
      panel3.add(hostTF);
      panel3.add(portTF);

      setTitle("Proxy info");
      pack();


      cancelButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event)
        {
          dispose();
          setVisible(false);
        }
      });

      okButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event)
        {
          proxy = hostTF.getText();
          port = Integer.parseInt(portTF.getText());
          dispose();
          setVisible(false);
        }
      });

    }
  }

  class Client extends Thread {

    private String proxy, host, url;
    private int port ;
    public String entete, body;

    Client ( String params[] ) {
      proxy = params[0];
      port = Integer.parseInt(params[1]);
      host = params[2];
      url = params[3];
      entete = "";
      body = "";
    }

    public void run() {
      Socket socketToWeb;
      PrintWriter toWeb;
      BufferedReader fromWeb;
      String str = "";
      int nb = 0;

      try {
        socketToWeb = new Socket(InetAddress.getByName(proxy),port);
        toWeb = new PrintWriter(
                    new BufferedWriter (
                        new OutputStreamWriter (
                            socketToWeb.getOutputStream())),true);
        fromWeb = new BufferedReader(
                    new InputStreamReader (
                            socketToWeb.getInputStream()));

        str = "GET http://"+host+url+" HTTP/1.1\r\nHost: "+host+
              "\r\nAccept: image/gif, image/x-xbitmap, image/jpeg,"+
              " image/pjpeg, image/png, */*\r\n";

        toWeb.println(str);

        boolean isHeader = true;

        int size = 0;
        while ( true ) {
         if ( isHeader ) {
            str = fromWeb.readLine();
            if ( str == null ) break;
            if ( str.length() == 0 ) {
              isHeader = false; // end of header
            }
            entete += str + "\r\n";
         } else {
            str = fromWeb.readLine();
            if ( str == null ) break;
            body += str + "\r\n";
         }
        }
      } catch ( IOException e ) {}

      Jentete.setText(entete);
      Jbody.setText(body);
      status.setText("Ready ...");
    }
  }

  private String proxy = "proxy";
  private int port = 8080;
  private JCheckBox useProxy = new JCheckBox("", true);

  private JLabel status = new JLabel("Ready...");
  private JTextField TFhost = new JTextField(13);
  private JTextField TFpath = new JTextField(17);

  private JTextArea Jentete = new JTextArea(10,30);
  private JTextArea Jbody = new JTextArea(30,30);
  private Client myClient;

  public GClient ()
  {
    JMenuBar menuBar = new JMenuBar();
    JButton closeButton = new JButton("Close");

    JPanel statusbar = new JPanel();
    JPanel Core = new JPanel();
    JPanel Result = new JPanel();
    JScrollPane SPentete;
    JScrollPane SPbody;

    Core.setLayout(new BorderLayout());
    Result.setLayout(new BorderLayout());

    Container contentPane = this.getContentPane();
    contentPane.setLayout(new BorderLayout());

    setJMenuBar(menuBar);

    JMenu fMenu = new JMenu("File");
    menuBar.add(fMenu);

    JMenuItem proxyItem = new JMenuItem("Set Proxy", 2);
    fMenu.add(proxyItem);
    fMenu.addSeparator();
    JMenuItem exitItem = new JMenuItem("Exit", 2);
    fMenu.add(exitItem);

    statusbar.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
    statusbar.setBorder(new BevelBorder(BevelBorder.LOWERED));
    contentPane.add(statusbar,BorderLayout.SOUTH );
    statusbar.add(status);


    JPanel url = new JPanel(new FlowLayout());

    JLabel Lurl = new JLabel("URL :");
    JButton getButton = new JButton("GET");

    url.add( Lurl ); url.add( TFhost );
    url.add( TFpath ); url.add( getButton );

    Core.add(url, BorderLayout.NORTH);

    SPentete = new JScrollPane(Jentete,
                               JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                               JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    SPbody = new JScrollPane(Jbody,
                             JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                             JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    Result.add(SPentete, BorderLayout.NORTH);
    Result.add(new JSeparator());
    Result.add(SPbody, BorderLayout.SOUTH);

    Core.add(Result, BorderLayout.SOUTH);

    contentPane.add(Core);


    // add listeners for the window and the various buttons
    this.addWindowListener( new WindowAdapter(){
         public void windowClosing(WindowEvent e){
              setVisible(false);
              dispose();
              System.exit(0);
    }});

    exitItem.addActionListener( new ActionListener(){
         public void actionPerformed(ActionEvent e){
              System.exit(0);
    }});

    getButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            getPage();
    }});

    proxyItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            changeProxy();
    }});

    pack();

  }

  protected void changeProxy()
  {
    new ProxyDialog(this, true).show();
  }

  private void getPage()
  {
    String[] params = new String[4];

    if ( useProxy.isSelected() )
    {
      params[0] = proxy ;
      params[1] = "" + port;
    }
    else
    {
      params[0] = TFhost.getText();
      params[1] = "80";
    }
    params[2] = TFhost.getText();
    params[3] = TFpath.getText();

    Client c = new Client(params);

    status.setText("Send Request to "+ params[0] +":"+params[1]+"...");

    c.start();

  }

  public static void main ( String args[] )
  {
    (new GClient()).setVisible(true);

  }

}