1 31 32 import java.awt.BorderLayout ; 33 import java.awt.FlowLayout ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.WindowAdapter ; 37 import java.awt.event.WindowEvent ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 41 import javax.swing.JButton ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JEditorPane ; 44 import javax.swing.JFrame ; 45 import javax.swing.JLabel ; 46 import javax.swing.JPanel ; 47 import javax.swing.JScrollPane ; 48 import javax.swing.JSplitPane ; 49 import javax.swing.JTextArea ; 50 import javax.swing.SwingUtilities ; 51 import javax.swing.text.BadLocationException ; 52 import javax.swing.text.html.HTMLDocument ; 53 54 import org.apache.commons.httpclient.HttpClient; 55 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 56 import org.apache.commons.httpclient.methods.GetMethod; 57 58 67 public class ClientApp { 68 69 public static void main(String [] args) { 70 HttpClientFrame f = new HttpClientFrame(); 71 f.setTitle("HttpClient demo application"); 72 f.setSize(700, 500); 73 f.addWindowListener( 74 new WindowAdapter () { 75 public void windowClosing(WindowEvent e) { 76 System.exit(0); 77 } 78 } 79 ); 80 f.setVisible(true); 81 } 82 83 public static class HttpClientFrame extends JFrame { 84 85 private JComboBox cmbURL; 86 private JTextArea taTextResponse; 87 private JEditorPane htmlPane; 88 89 private HttpClient client; 90 91 public HttpClientFrame() { 92 client = new HttpClient(new MultiThreadedHttpConnectionManager()); 93 client.getHttpConnectionManager(). 94 getParams().setConnectionTimeout(30000); 95 96 JPanel panInput = new JPanel (new FlowLayout ()); 97 98 String [] aURLs = { 99 "http://www.apache.org/", 100 "http://www.google.com/", 101 "http://www.opensource.org/", 102 "http://www.anybrowser.org/", 103 "http://jakarta.apache.org/", 104 "http://www.w3.org/" 105 }; 106 107 final JButton btnGET = new JButton ("GET"); 108 btnGET.addActionListener( 109 new ActionListener () { 110 public void actionPerformed(ActionEvent ae) { 111 String url = (String ) cmbURL.getSelectedItem(); 112 if (url != null && url.length() > 0) { 113 loadPage(url); 114 } 115 } 116 } 117 ); 118 119 cmbURL = new JComboBox (aURLs); 120 cmbURL.setToolTipText("Enter a URL"); 121 cmbURL.setEditable(true); 122 cmbURL.setSelectedIndex(0); 123 124 JLabel lblURL = new JLabel ("URL:"); 125 126 panInput.add(lblURL); 127 panInput.add(cmbURL); 128 panInput.add(btnGET); 129 130 taTextResponse = new JTextArea (); 131 taTextResponse.setEditable(false); 132 taTextResponse.setCaretPosition(0); 133 134 htmlPane = new JEditorPane (); 135 htmlPane.setContentType("text/html"); 136 htmlPane.setEditable(false); 137 138 JSplitPane splitResponsePane = new JSplitPane ( 139 JSplitPane.HORIZONTAL_SPLIT, 140 new JScrollPane (taTextResponse), 141 new JScrollPane (htmlPane) 142 ); 143 splitResponsePane.setOneTouchExpandable(false); 144 splitResponsePane.setDividerLocation(350); 145 149 this.getContentPane().setLayout(new BorderLayout ()); 150 this.getContentPane().add(panInput, BorderLayout.NORTH); 151 this.getContentPane().add(splitResponsePane, BorderLayout.CENTER); 152 } 153 154 159 private void setDocumentContent(String content) { 160 161 HTMLDocument doc = new HTMLDocument (); 162 try { 163 doc.remove(0, doc.getLength()); 164 } catch (BadLocationException e) { 165 e.printStackTrace(); 166 } 167 doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); 168 169 try { 170 htmlPane.read(new ByteArrayInputStream (content.getBytes()), doc); 171 } catch (IOException e) { 172 e.printStackTrace(); 173 } 174 175 htmlPane.setDocument(doc); 176 htmlPane.setCaretPosition(0); 177 178 taTextResponse.setText(content); 179 taTextResponse.setCaretPosition(0); 180 taTextResponse.requestFocus(); 181 } 182 183 187 private void loadPage(final String url) { 188 new Thread () { 190 public void run() { 191 GetMethod get = new GetMethod(url); 192 get.setFollowRedirects(true); 193 194 try { 195 int iGetResultCode = client.executeMethod(get); 196 final String strGetResponseBody = get.getResponseBodyAsString(); 197 198 if (strGetResponseBody != null) { 199 SwingUtilities.invokeLater( 201 new Runnable () { 202 public void run() { 203 setDocumentContent(strGetResponseBody); 204 } 205 } 206 ); 207 } 208 } catch (Exception ex) { 209 ex.printStackTrace(); 210 } finally { 211 get.releaseConnection(); 212 } 213 } 214 }.start(); 215 } 216 217 } 218 219 } 220 | Popular Tags |