import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringReader; import java.net.Socket; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /* * Created on 2005/07/12 * * Simple Web Client * * copyright(c) 2005 YAGI Hiroto All Right Reserved * * 2005/07/12 ver 0.5 * */ public class SimpleWebClient extends JFrame { private final String[] HTTP_METHOD = { "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT" }; private final String[] HTTP_VERSION = { "HTTP/1.0", "HTTP/1.1" }; private JComboBox cmbMethod = null; private JComboBox cmbHttpVer = null; private JTextField txtRequestUri = null; private JTextField txtHost = null; private JTextField txtPort = null; private JTextArea txtRequest = null; private JTextArea txtResult = null; private JButton btnRequest = null; public SimpleWebClient() { super(); initialize(); } private void initialize() { Container container = this.getContentPane(); this.setSize(600,400); this.setTitle("Simple Web Client"); container.setLayout(new BorderLayout()); container.add(getControlPane(), BorderLayout.NORTH); container.add(getResultTextArea(), BorderLayout.CENTER); } private JPanel getControlPane() { JPanel controlPane = new JPanel(); controlPane.setLayout(new BoxLayout(controlPane,BoxLayout.Y_AXIS)); JPanel chosePane = new JPanel(); chosePane.setLayout(new BoxLayout(chosePane, BoxLayout.X_AXIS)); chosePane.add(new JLabel(" http method : ")); chosePane.add(getMethodCombo()); chosePane.add(new JLabel(" request-uri : http://")); chosePane.add(getRequestUriTextField()); chosePane.add(new JLabel(" http-version : ")); chosePane.add(getHttpVer()); JPanel hostInfoPane = new JPanel(); hostInfoPane.setLayout(new BoxLayout(hostInfoPane, BoxLayout.X_AXIS)); hostInfoPane.add(new JLabel(" host : ")); hostInfoPane.add(getHostTextField()); hostInfoPane.add(new JLabel(" port : ")); hostInfoPane.add(getPortTextField()); hostInfoPane.add(getRequestButton()); JPanel requestPane = new JPanel(); requestPane.setLayout(new BoxLayout(requestPane, BoxLayout.X_AXIS)); requestPane.add(getRequextTextArea()); controlPane.add(chosePane); controlPane.add(requestPane); controlPane.add(hostInfoPane); return controlPane; } private JComboBox getMethodCombo() { cmbMethod = new JComboBox(HTTP_METHOD); cmbMethod.addActionListener(new RequestLineChangeLisntner()); return cmbMethod; } private JComboBox getHttpVer() { cmbHttpVer = new JComboBox(HTTP_VERSION); cmbHttpVer.addActionListener(new RequestLineChangeLisntner()); return cmbHttpVer; } private JTextField getRequestUriTextField() { txtRequestUri = new JTextField("localhost:8080/index.jsp", 20); txtRequestUri.addActionListener(new RequestLineChangeLisntner()); return txtRequestUri; } private JTextField getHostTextField() { txtHost = new JTextField("localhost",10); return txtHost; } private JTextField getPortTextField() { txtPort = new JTextField("8080", 4); return txtPort; } private JScrollPane getRequextTextArea() { txtRequest = new JTextArea(4,20); return new JScrollPane(txtRequest); } private JScrollPane getResultTextArea() { txtResult = new JTextArea(20,20); return new JScrollPane(txtResult); } private JButton getRequestButton() { btnRequest = new JButton("request"); btnRequest.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { Thread th = new Thread(new RequestProcess()); th.run(); } } ); return btnRequest; } private void changeRequestLine() { try { StringBuffer request = new StringBuffer(); request.append(HTTP_METHOD[cmbMethod.getSelectedIndex()]); request.append(" "); request.append("http://"); request.append(txtRequestUri.getText()); request.append(" "); request.append(HTTP_VERSION[cmbHttpVer.getSelectedIndex()]); request.append("\n"); BufferedReader reader = new BufferedReader(new StringReader(txtRequest.getText())); String line = null; int linCnt = 0; while((line = reader.readLine()) != null) { if (linCnt > 0) { request.append(line); request.append("\n"); } linCnt++; } txtRequest.setText(request.toString()); }catch (IOException ioe) { ioe.printStackTrace(); txtResult.setText(ioe.toString()); } } private class RequestLineChangeLisntner implements ActionListener { public void actionPerformed(ActionEvent e) { changeRequestLine(); } } private class RequestProcess implements Runnable { public void run() { Socket sok = null; PrintWriter out = null; BufferedReader in = null; try { txtResult.setText(""); btnRequest.setEnabled(false); String host = txtHost.getText(); int port = Integer.parseInt(txtPort.getText()); sok = new Socket(host ,port); out = new PrintWriter(sok.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(sok.getInputStream())); String requestdata = txtRequest.getText(); if (requestdata.trim().length() == 0) { changeRequestLine(); requestdata = txtRequest.getText(); } StringBuffer request = new StringBuffer(); BufferedReader reader = new BufferedReader(new StringReader(requestdata)); String line = null; int linCnt = 0; while((line = reader.readLine()) != null) { request.append(line); request.append("\r\n"); } out.println(request); out.flush(); StringBuffer response = new StringBuffer(); while((line = in.readLine()) != null) { response.append(line); response.append("\n"); } txtResult.setText(response.toString()); }catch(Exception e) { e.printStackTrace(); txtResult.setText(e.toString()); }finally { btnRequest.setEnabled(true); try { if (in != null) in.close(); if (out != null) out.close(); if (sok != null) sok.close(); }catch(Exception e){} } } } public static void main(String[] args) { SimpleWebClient me = new SimpleWebClient(); me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); me.setVisible(true); } }