我没怎么细读源码,等下次详细看的时候将这句话去掉。

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.net.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. /** Panel for selecting the format of the query text, either as
  9. * name/value pairs or raw text (for example, sending a
  10. * serialized object.
  11. * <P>
  12. * Also, provides the ability to encode a String in the
  13. * application/x-www-form-urlencoded format.
  14. * <P>
  15. * Taken from Core Servlets and JavaServer Pages Volume II
  16. * from Prentice Hall and Sun Microsystems Press,
  17. * http://volume2.coreservlets.com/.
  18. * (C) 2007 Marty Hall, Larry Brown, and Yaakov Chaikin;
  19. * may be freely used or adapted.
  20. */
  21.  
  22. public class EncodeQueryPanel extends JPanel
  23. implements ActionListener {
  24. private Font labelFont, textFont;
  25. private JButton okButton, cancelButton;
  26. private JRadioButton optionPair, optionRaw;
  27. private int value;
  28. private Window window;
  29.  
  30. public EncodeQueryPanel(Window window) {
  31. this.window = window;
  32. labelFont = new Font("Serif", Font.BOLD, 14);
  33. textFont = new Font("Monospaced", Font.BOLD, 12);
  34. setLayout(new BorderLayout());
  35. add(getOptionPanel(), BorderLayout.CENTER);
  36. add(getButtonPanel(), BorderLayout.SOUTH);
  37. value = JOptionPane.CANCEL_OPTION;
  38. }
  39.  
  40. private JPanel getOptionPanel() {
  41. JPanel optionPanel = new JPanel();
  42. Border border = BorderFactory.createEtchedBorder();
  43. optionPanel.setBorder(
  44. BorderFactory.createTitledBorder(border,
  45. "Encode data as ... ",
  46. TitledBorder.LEFT,
  47. TitledBorder.CENTER,
  48. labelFont));
  49. optionPanel.setLayout(
  50. new BoxLayout(optionPanel, BoxLayout.Y_AXIS));
  51. optionPair = new JRadioButton("name/value pairs");
  52. optionPair.setFont(labelFont);
  53. optionPair.setSelected(true);
  54. optionRaw = new JRadioButton("raw text");
  55. optionRaw.setFont(labelFont);
  56. ButtonGroup group = new ButtonGroup();
  57. group.add(optionPair);
  58. group.add(optionRaw);
  59. optionPanel.add(optionPair);
  60. optionPanel.add(optionRaw);
  61. return(optionPanel);
  62. }
  63.  
  64. private JPanel getButtonPanel() {
  65. JPanel buttonPanel = new JPanel();
  66. okButton = new JButton("OK");
  67. okButton.setFont(labelFont);
  68. okButton.addActionListener(this);
  69. cancelButton = new JButton("Cancel");
  70. cancelButton.setFont(labelFont);
  71. cancelButton.addActionListener(this);
  72. buttonPanel.add(okButton);
  73. buttonPanel.add(cancelButton);
  74. return(buttonPanel);
  75. }
  76.  
  77. public void actionPerformed(ActionEvent event) {
  78. if (event.getSource() == okButton) {
  79. value = JOptionPane.OK_OPTION;
  80. }
  81. window.dispose();
  82. }
  83.  
  84. public int getValue() {
  85. return(value);
  86. }
  87.  
  88. /** Based on option selected (name/value pairs, raw text),
  89. * encode the data (assume UTF-8 charset).
  90. */
  91.  
  92. public String encode(String queryData)
  93. throws UnsupportedEncodingException {
  94. if(queryData == null || queryData.length() == 0) {
  95. return(queryData);
  96. }
  97. if (optionRaw.isSelected()) {
  98. queryData = URLEncoder.encode(queryData, "UTF-8");
  99. } else {
  100. // Fit each name/value pair and rebuild with
  101. // the value encoded.
  102. StringBuffer encodedData = new StringBuffer();
  103. String[] pairs = queryData.split("&");
  104. for(int i=0; i<pairs.length; i++) {
  105. encodedData.append(encodePair(pairs[i]));
  106. if (i<pairs.length-1) {
  107. encodedData.append("&");
  108. }
  109. }
  110. queryData = encodedData.toString();
  111. }
  112. return(queryData);
  113. }
  114.  
  115. // Process name/value pair, returning pair with
  116. // value encoded.
  117.  
  118. private String encodePair(String nameValuePair)
  119. throws UnsupportedEncodingException {
  120. String encodedPair = "";
  121. String[] pair = nameValuePair.split("=");
  122. if (pair[0].trim().length() == 0) {
  123. throw new UnsupportedEncodingException("Name missing");
  124. }
  125. encodedPair = pair[0].trim() + "=";
  126. if (pair.length > 1) {
  127. encodedPair += URLEncoder.encode(pair[1], "UTF-8");
  128. }
  129. return(encodedPair);
  130. }
  131. }

EncodeQueryPanel

  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.net.*;
  5. import javax.net.ssl.*;
  6. import javax.swing.*;
  7.  
  8. /** The underlying network client used by WebClient. Sends an
  9. * HTTP request in the following format:<P>
  10. *
  11. * GET / HTTP/1.0
  12. * <P>
  13. * Request can be GET or POST, and the HTTP version can be 1.0
  14. * or 1.1 (a Host: header is required for HTTP 1.1).
  15. * Supports both HTTP and HTTPS (SSL).
  16. * <P>
  17. * Taken from Core Servlets and JavaServer Pages Volume II
  18. * from Prentice Hall and Sun Microsystems Press,
  19. * http://volume2.coreservlets.com/.
  20. * (C) 2007 Marty Hall, Larry Brown, and Yaakov Chaikin;
  21. * may be freely used or adapted.
  22. */
  23.  
  24. public class HttpClient {
  25. protected URL url;
  26. protected String requestMethod;
  27. protected String httpVersion;
  28. protected List requestHeaders;
  29. protected String queryData;
  30. protected JTextArea outputArea;
  31. protected boolean interrupted;
  32.  
  33. public HttpClient(URL url,
  34. String requestMethod,
  35. String httpVersion,
  36. List requestHeaders,
  37. String queryData,
  38. JTextArea outputArea) {
  39. this.url = url;
  40. this.requestMethod = requestMethod;
  41. this.httpVersion = httpVersion;
  42. this.requestHeaders = requestHeaders;
  43. this.queryData = queryData;
  44. this.outputArea = outputArea;
  45. }
  46.  
  47. /** Establish the connection, then pass the socket
  48. * to handleConnection.
  49. */
  50.  
  51. public void connect() {
  52. if(!isValidURL()) {
  53. return;
  54. }
  55. String host = url.getHost();
  56. int port = url.getPort();
  57. if (port == -1) {
  58. port = url.getDefaultPort();
  59. }
  60. connect(host, port);
  61. }
  62.  
  63. /** Open a TCP connection to host on specified port and
  64. * then call handleConnection to process the request.
  65. * For an https request, use a SSL socket.
  66. */
  67.  
  68. protected void connect(String host, int port) {
  69. try {
  70. Socket client = null;
  71. if (isSecure()) {
  72. SocketFactory factory = SSLSocketFactory.getDefault();
  73. client = factory.createSocket(host, port);
  74. } else {
  75. client = new Socket(host, port);
  76. }
  77. handleConnection(client);
  78. client.close();
  79. } catch(UnknownHostException uhe) {
  80. report("Unknown host: " + host);
  81. uhe.printStackTrace();
  82. } catch(ConnectException ce) {
  83. report("Connection problem: " + ce.getMessage());
  84. ce.printStackTrace();
  85. } catch(IOException ioe) {
  86. report("IOException: " + ioe.getMessage());
  87. ioe.printStackTrace();
  88. }
  89. }
  90.  
  91. /** Send request to server, providing all specified headers
  92. * and query data. If a POST request, add a header for the
  93. * Content-Length.
  94. */
  95.  
  96. public void handleConnection(Socket socket) {
  97. try {
  98. // Make a PrintWriter to send outgoing data.
  99. // Second argument of true means autoflush.
  100. PrintWriter out =
  101. new PrintWriter(socket.getOutputStream(), true);
  102. // Make a BufferedReader to get incoming data.
  103. BufferedReader in =
  104. new BufferedReader(
  105. new InputStreamReader(socket.getInputStream()));
  106. StringBuffer buffer = new StringBuffer();
  107. outputArea.setText("");
  108. buffer.append(getRequestLine() + "\r\n");
  109. for(int i=0; i<requestHeaders.size(); i++) {
  110. buffer.append(requestHeaders.get(i) + "\r\n");
  111. }
  112. // Add Content-Length header for POST data.
  113. if ("POST".equalsIgnoreCase(requestMethod)) {
  114. buffer.append("Content-Length: " +
  115. queryData.length() + "\r\n");
  116. buffer.append("\r\n");
  117. buffer.append(queryData);
  118. } else {
  119. buffer.append("\r\n");
  120. }
  121. System.out.println("Request:\n\n" + buffer.toString());
  122. out.println(buffer.toString());
  123. out.flush();
  124. String line;
  125. while ((line = in.readLine()) != null &&
  126. !interrupted) {
  127. outputArea.append(line + "\n");
  128. }
  129. if (interrupted) {
  130. outputArea.append("---- Download Interrupted ----");
  131. }
  132. out.close();
  133. in.close();
  134. } catch(Exception e) {
  135. outputArea.setText("Error: " + e);
  136. }
  137. }
  138.  
  139. /** Create HTTP request line, i.e., GET URI HTTP/1.0 */
  140.  
  141. protected String getRequestLine() {
  142. String method = "GET";
  143. String uri = url.getPath();
  144. String version = "HTTP/1.0";
  145. // Determine if POST request. If not, then GET request.
  146. // Add query data after ? for GET request.
  147. if ("POST".equalsIgnoreCase(requestMethod)) {
  148. method = "POST";
  149. } else {
  150. if (queryData.length() > 0) {
  151. uri += "?" + queryData;
  152. }
  153. }
  154. if ("HTTP/1.1".equalsIgnoreCase(httpVersion)) {
  155. version = "HTTP/1.1";
  156. }
  157. String request = method + " " + uri + " " + version;
  158. return(request);
  159. }
  160.  
  161. protected void report(String str) {
  162. outputArea.setText(str);
  163. }
  164.  
  165. /* Check protocol for https (SSL). */
  166.  
  167. protected boolean isSecure() {
  168. return("https".equalsIgnoreCase(url.getProtocol()));
  169. }
  170.  
  171. public void setInterrupted(boolean interrupted) {
  172. this.interrupted = interrupted;
  173. }
  174.  
  175. /** Determine if host evaluates to an Internet address. */
  176.  
  177. protected boolean isValidURL() {
  178. if (url == null) {
  179. return(false);
  180. }
  181. try {
  182. InetAddress.getByName(url.getHost());
  183. return(true);
  184. } catch(UnknownHostException uhe) {
  185. report("Bogus Host: " + url.getHost());
  186. return(false);
  187. }
  188. }
  189. }

HttpClient

  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.net.*;
  5. import javax.net.ssl.*;
  6. import javax.swing.*;
  7.  
  8. /** The underlying proxy client used by WebClient. Proxy
  9. * requests are sent in the following format:<P>
  10. *
  11. * GET URL HTTP/1.0
  12. *
  13. * <P>where the URL is the WebClient URL, for example,
  14. * http://www.google.com/.
  15. * <P>
  16. * Taken from Core Servlets and JavaServer Pages Volume II
  17. * from Prentice Hall and Sun Microsystems Press,
  18. * http://volume2.coreservlets.com/.
  19. * (C) 2007 Marty Hall, Larry Brown, and Yaakov Chaikin;
  20. * may be freely used or adapted.
  21. */
  22.  
  23. public class HttpProxyClient extends HttpClient {
  24. private URL proxyURL;
  25.  
  26. public HttpProxyClient(URL url,
  27. URL proxyURL,
  28. String requestMethod,
  29. String httpVersion,
  30. List requestHeaders,
  31. String queryData,
  32. JTextArea outputArea) {
  33. super(url, requestMethod, httpVersion,
  34. requestHeaders, queryData, outputArea);
  35. this.proxyURL = proxyURL;
  36. }
  37.  
  38. /** Open TCP connection to Proxy host. */
  39.  
  40. public void connect() {
  41. if(!isValidURL() || !isValidProxyURL()) {
  42. return;
  43. }
  44. String host = proxyURL.getHost();
  45. int port = proxyURL.getPort();
  46. if (port == -1) {
  47. port = proxyURL.getDefaultPort();
  48. }
  49. connect(host, port);
  50. }
  51.  
  52. /** Create HTTP request line for proxy server. Instead of
  53. * stating a URI, the GET or POST request states the full
  54. * URL for the original page request. For example, <P>
  55. *
  56. * GET http://www.google.com/ HTTP/1.0
  57. */
  58.  
  59. protected String getRequestLine() {
  60. String method = "GET";
  61. String url = this.url.toString();
  62. String version = "HTTP/1.0";
  63. // Determine if POST request. If not, then GET request.
  64. // Add query data after ? for GET request.
  65. if ("POST".equalsIgnoreCase(requestMethod)) {
  66. method = "POST";
  67. } else {
  68. if (queryData.length() > 0) {
  69. url += "?" + queryData;
  70. }
  71. }
  72. if ("HTTP/1.1".equalsIgnoreCase(httpVersion)) {
  73. version = "HTTP/1.1";
  74. }
  75. String request = method + " " + url + " " + version;
  76. return(request);
  77. }
  78.  
  79. /** Determine if proxy server is a valid host address. */
  80.  
  81. protected boolean isValidProxyURL() {
  82. if (proxyURL == null) {
  83. return(false);
  84. }
  85. try {
  86. InetAddress.getByName(proxyURL.getHost());
  87. return(true);
  88. } catch(UnknownHostException uhe) {
  89. report("Bogus Proxy: " + url.getHost());
  90. return(false);
  91. }
  92. }
  93. }

HttpProxyClient

  1. import java.awt.*; // For FlowLayout, Font.
  2. import javax.swing.*;
  3.  
  4. /** A TextField with an associated Label.
  5. * <P>
  6. * Taken from Core Servlets and JavaServer Pages Volume II
  7. * from Prentice Hall and Sun Microsystems Press,
  8. * http://volume2.coreservlets.com/.
  9. * (C) 2007 Marty Hall, Larry Brown, and Yaakov Chaikin;
  10. * may be freely used or adapted.
  11. */
  12.  
  13. public class LabeledTextField extends JPanel {
  14. private JLabel label;
  15. private JTextField textField;
  16.  
  17. public LabeledTextField(String labelString,
  18. Font labelFont,
  19. int textFieldSize,
  20. Font textFont) {
  21. setLayout(new FlowLayout(FlowLayout.LEFT));
  22. label = new JLabel(labelString, JLabel.RIGHT);
  23. if (labelFont != null) {
  24. label.setFont(labelFont);
  25. }
  26. add(label);
  27. textField = new JTextField(textFieldSize);
  28. if (textFont != null) {
  29. textField.setFont(textFont);
  30. }
  31. add(textField);
  32. }
  33.  
  34. public LabeledTextField(String labelString,
  35. String textFieldString) {
  36. this(labelString, null, textFieldString,
  37. textFieldString.length(), null);
  38. }
  39.  
  40. public LabeledTextField(String labelString,
  41. int textFieldSize) {
  42. this(labelString, null, textFieldSize, null);
  43. }
  44.  
  45. public LabeledTextField(String labelString,
  46. Font labelFont,
  47. String textFieldString,
  48. int textFieldSize,
  49. Font textFont) {
  50. this(labelString, labelFont,
  51. textFieldSize, textFont);
  52. textField.setText(textFieldString);
  53. }
  54.  
  55. /** The Label at the left side of the LabeledTextField.
  56. * To manipulate the Label, do:
  57. * <PRE>
  58. * LabeledTextField ltf = new LabeledTextField(...);
  59. * ltf.getLabel().someLabelMethod(...);
  60. * </PRE>
  61. */
  62.  
  63. public JLabel getLabel() {
  64. return(label);
  65. }
  66.  
  67. /** The TextField at the right side of the
  68. * LabeledTextField.
  69. */
  70.  
  71. public JTextField getTextField() {
  72. return(textField);
  73. }
  74.  
  75. public void setText(String textFieldString) {
  76. textField.setText(textFieldString);
  77. }
  78. }

LabelTestField

main程序:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7.  
  8. /** A graphical client that lets you interactively connect to
  9. * Web servers and send custom URLs, request headers, and
  10. * query data. The user can optionally select a GET or POST
  11. * request and HTTP version 1.0 or 1.1.
  12. * <P>
  13. * For an HTTPS connection, you can specify a nondefault
  14. * keystore through system properties on the command line,
  15. * i.e.,
  16. * <P>
  17. * java -Djavax.net.ssl.trustStore=server.ks
  18. * -Djavax.net.ssl.trustStoreType=JKS
  19. * <P>
  20. * Taken from Core Servlets and JavaServer Pages Volume II
  21. * from Prentice Hall and Sun Microsystems Press,
  22. * http://volume2.coreservlets.com/.
  23. * (C) 2007 Marty Hall, Larry Brown, and Yaakov Chaikin;
  24. * may be freely used or adapted.
  25. */
  26.  
  27. public class WebClient extends JPanel implements Runnable {
  28. public static void main(String[] args) {
  29. if (args.length > 0) {
  30. usage();
  31. } else {
  32. try {
  33. UIManager.setLookAndFeel(
  34. UIManager.getSystemLookAndFeelClassName());
  35. } catch(Exception e) {
  36. System.out.println("Error setting native LAF: " + e);
  37. }
  38. Container content = new WebClient();
  39. content.setBackground(SystemColor.control);
  40. JFrame frame = new JFrame("Web Client");
  41. frame.setContentPane(content);
  42. frame.setBackground(SystemColor.control);
  43. frame.setSize(600, 700);
  44. frame.setLocationRelativeTo(null);
  45. frame.setDefaultCloseOperation(
  46. WindowConstants.EXIT_ON_CLOSE);
  47. frame.setVisible(true);
  48. }
  49. }
  50. private static JFrame frame;
  51. private LabeledTextField urlField;
  52. private JComboBox methodCombo, versionCombo;
  53. private LabeledTextField proxyHostField, proxyPortField;
  54. private JTextArea requestHeadersArea, queryDataArea;
  55. private JTextArea resultArea;
  56. private JButton encodeButton, submitButton, interruptButton;
  57. private Font labelFont, headingFont, textFont;
  58. private HttpClient client;
  59.  
  60. public WebClient() {
  61. int fontSize = 14;
  62. labelFont = new Font("Serif", Font.BOLD, fontSize);
  63. headingFont = new Font("SansSerif", Font.BOLD, fontSize+4);
  64. textFont = new Font("Monospaced", Font.BOLD, fontSize-2);
  65. setLayout(new BorderLayout(5, 30));
  66. // Set up URL, Request Method, and Proxy.
  67. JPanel topPanel = new JPanel(new GridLayout(3,1));
  68. topPanel.add(getURLPanel());
  69. topPanel.add(getRequestMethodPanel());
  70. topPanel.add(getProxyPanel());
  71. // Set up Request Header and Query Data.
  72. JPanel inputPanel = new JPanel(new GridLayout(3,1));
  73. inputPanel.add(topPanel);
  74. inputPanel.add(getRequestHeaderPanel());
  75. inputPanel.add(getQueryDataPanel());
  76. add(inputPanel, BorderLayout.NORTH);
  77. add(getResultPanel(), BorderLayout.CENTER);
  78. }
  79.  
  80. private JPanel getURLPanel() {
  81. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  82. urlField =
  83. new LabeledTextField("URL:", labelFont, 75, textFont);
  84. panel.add(urlField);
  85. return(panel);
  86. }
  87.  
  88. private JPanel getRequestMethodPanel() {
  89. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  90. JLabel methodLabel = new JLabel(" Request Method:");
  91. methodLabel.setFont(labelFont);
  92. panel.add(methodLabel);
  93. methodCombo = new JComboBox();
  94. methodCombo.addItem("GET");
  95. methodCombo.addItem("POST");
  96. panel.add(methodCombo);
  97. JLabel versionlabel = new JLabel(" HTTP Version:");
  98. versionlabel.setFont(labelFont);
  99. panel.add(versionlabel);
  100. versionCombo = new JComboBox();
  101. versionCombo.addItem("HTTP/1.0");
  102. versionCombo.addItem("HTTP/1.1");
  103. panel.add(versionCombo);
  104. return(panel);
  105. }
  106.  
  107. private JPanel getProxyPanel() {
  108. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  109. proxyHostField =
  110. new LabeledTextField("Proxy Host:", labelFont,
  111. 35, textFont);
  112. proxyPortField =
  113. new LabeledTextField("Proxy Port:", labelFont,
  114. 5, textFont);
  115. panel.add(proxyHostField);
  116. panel.add(proxyPortField);
  117. // Check to see if command-line system properties are set
  118. // for proxy.
  119. String proxyHost = System.getProperty("http.proxyHost");
  120. String sslProxyHost = System.getProperty("https.proxyHost");
  121. String proxyPort = System.getProperty("http.proxyPort");
  122. String sslProxyPort = System.getProperty("https.proxyPort");
  123. if (proxyHost != null) {
  124. proxyHostField.setText(proxyHost);
  125. if (proxyPort != null) {
  126. proxyPortField.setText(proxyPort);
  127. }
  128. } else if (sslProxyHost != null) {
  129. proxyHostField.setText(sslProxyHost);
  130. if (sslProxyPort != null) {
  131. proxyPortField.setText(sslProxyPort);
  132. }
  133. }
  134. return(panel);
  135. }
  136.  
  137. private JPanel getRequestHeaderPanel() {
  138. JPanel panel = new JPanel();
  139. panel.setLayout(new BorderLayout());
  140. JLabel requestLabel = new JLabel("Request Headers:");
  141. requestLabel.setFont(labelFont);
  142. panel.add(requestLabel, BorderLayout.NORTH);
  143. requestHeadersArea = new JTextArea(5, 80);
  144. requestHeadersArea.setFont(textFont);
  145. JScrollPane headerScrollArea =
  146. new JScrollPane(requestHeadersArea);
  147. panel.add(headerScrollArea, BorderLayout.CENTER);
  148. return(panel);
  149. }
  150.  
  151. private JPanel getQueryDataPanel() {
  152. JPanel panel = new JPanel();
  153. panel.setLayout(new BorderLayout());
  154. JLabel formLabel = new JLabel("Query Data:");
  155. formLabel.setFont(labelFont);
  156. panel.add(formLabel, BorderLayout.NORTH);
  157. queryDataArea = new JTextArea(3, 80);
  158. queryDataArea.setFont(textFont);
  159. JScrollPane formScrollArea =
  160. new JScrollPane(queryDataArea);
  161. panel.add(formScrollArea, BorderLayout.CENTER);
  162. panel.add(getButtonPanel(), BorderLayout.SOUTH);
  163. return(panel);
  164. }
  165.  
  166. private JPanel getButtonPanel() {
  167. JPanel panel = new JPanel();
  168. encodeButton = new JButton("Encode Data");
  169. encodeButton.addActionListener(new EncodeListener());
  170. encodeButton.setFont(labelFont);
  171. panel.add(encodeButton);
  172. submitButton = new JButton("Submit Request");
  173. submitButton.addActionListener(new SubmitListener());
  174. submitButton.setFont(labelFont);
  175. panel.add(submitButton);
  176. return(panel);
  177. }
  178.  
  179. private JPanel getResultPanel() {
  180. JPanel resultPanel = new JPanel();
  181. resultPanel.setLayout(new BorderLayout());
  182. JLabel resultLabel =
  183. new JLabel("Results", JLabel.CENTER);
  184. resultLabel.setFont(headingFont);
  185. resultPanel.add(resultLabel, BorderLayout.NORTH);
  186. resultArea = new JTextArea();
  187. resultArea.setFont(textFont);
  188. JScrollPane resultScrollArea =
  189. new JScrollPane(resultArea);
  190. resultPanel.add(resultScrollArea, BorderLayout.CENTER);
  191. JPanel interruptPanel = new JPanel();
  192. interruptButton = new JButton("Interrupt Download");
  193. interruptButton.setFont(labelFont);
  194. interruptButton.addActionListener(new InterruptListener());
  195. interruptPanel.add(interruptButton);
  196. resultPanel.add(interruptPanel, BorderLayout.SOUTH);
  197. return(resultPanel);
  198. }
  199.  
  200. /** Create all inputs and then process the request either
  201. * directly (HttpClient) or through a proxy server
  202. * (HttpProxyClient).
  203. */
  204.  
  205. public void run() {
  206. if (hasLegalValues()) {
  207. URL url = getRequestURL();
  208. String requestMethod = getRequestMethod();
  209. String httpVersion = getHttpVersion();
  210. ArrayList requestHeaders = getRequestHeaders();
  211. String queryData = getQueryData();
  212. resultArea.setText("");
  213. if (usingProxy()) {
  214. URL proxyURL = getProxyURL();
  215. client = new HttpProxyClient(url, proxyURL,
  216. requestMethod, httpVersion,
  217. requestHeaders, queryData,
  218. resultArea);
  219. } else {
  220. client = new HttpClient(url,
  221. requestMethod, httpVersion,
  222. requestHeaders, queryData,
  223. resultArea);
  224. }
  225. client.connect();
  226. }
  227. }
  228.  
  229. public boolean usingProxy() {
  230. String proxyHost = getProxyHost();
  231. return(proxyHost != null && proxyHost.length() > 0);
  232. }
  233.  
  234. private boolean hasLegalValues() {
  235. if (getRequestURL() == null) {
  236. report("Malformed URL");
  237. return(false);
  238. }
  239. if (usingProxy() && getProxyURL() == null) {
  240. report("Proxy invalid");
  241. return(false);
  242. }
  243. return(true);
  244. }
  245.  
  246. // Turn proxy host and port into a URL.
  247.  
  248. private URL getProxyURL() {
  249. URL requestURL = getRequestURL();
  250. if (requestURL == null) {
  251. return(null);
  252. }
  253. String proxyURLStr = requestURL.getProtocol() +
  254. "://" + getProxyHost();
  255. String proxyPort = getProxyPort();
  256. if (proxyPort != null && proxyPort.length() > 0) {
  257. proxyURLStr += ":" + proxyPort + "/";
  258. }
  259. return(getURL(proxyURLStr));
  260. }
  261.  
  262. public URL getRequestURL() {
  263. return(getURL(urlField.getTextField().getText().trim()));
  264. }
  265.  
  266. public URL getURL(String str) {
  267. try {
  268. URL url = new URL(str);
  269. return(url);
  270. } catch(MalformedURLException mue) {
  271. return(null);
  272. }
  273. }
  274.  
  275. private String getRequestMethod() {
  276. return((String)methodCombo.getSelectedItem());
  277. }
  278.  
  279. private String getHttpVersion() {
  280. return((String)versionCombo.getSelectedItem());
  281. }
  282.  
  283. private String getProxyHost() {
  284. return(proxyHostField.getTextField().getText().trim());
  285. }
  286.  
  287. private String getProxyPort() {
  288. return(proxyPortField.getTextField().getText().trim());
  289. }
  290.  
  291. private ArrayList getRequestHeaders() {
  292. ArrayList requestHeaders = new ArrayList();
  293. int headerNum = 0;
  294. String header =
  295. requestHeadersArea.getText().trim();
  296. StringTokenizer tok =
  297. new StringTokenizer(header, "\r\n");
  298. while (tok.hasMoreTokens()) {
  299. requestHeaders.add(tok.nextToken());
  300. }
  301. return(requestHeaders);
  302. }
  303.  
  304. private String getQueryData() {
  305. return(queryDataArea.getText());
  306. }
  307.  
  308. private void report(String s) {
  309. resultArea.setText(s);
  310. }
  311.  
  312. private static void usage() {
  313. System.out.println(
  314. "Usage: java [-Djavax.net.ssl.trustStore=value] \n" +
  315. " [-Djavax.net.ssl.trustStoreType=value] \n" +
  316. " [-Dhttp.proxyHost=value] \n" +
  317. " [-Dhttp.proxyPort=value] \n" +
  318. " [-Dhttps.proxyHost=value] \n" +
  319. " [-Dhttps.proxyPort=value] WebClient");
  320. }
  321.  
  322. /** Listener for Submit button. Performs HTTP request on
  323. * separate thread.
  324. */
  325.  
  326. class SubmitListener implements ActionListener {
  327. public void actionPerformed(ActionEvent event) {
  328. Thread downloader = new Thread(WebClient.this);
  329. downloader.start();
  330. }
  331. }
  332.  
  333. /** Listener for Encode Data button. Open dialog to
  334. * determine how to encode the data (name/value pairs
  335. * or raw text).
  336. */
  337.  
  338. class EncodeListener implements ActionListener {
  339. public void actionPerformed(ActionEvent event) {
  340. String queryData = getQueryData();
  341. if (queryData.length() == 0) {
  342. return;
  343. }
  344. JDialog dialog = new JDialog(frame, "Encode", true);
  345. dialog.setDefaultCloseOperation(
  346. WindowConstants.DISPOSE_ON_CLOSE);
  347. dialog.setLocationRelativeTo(frame);
  348. EncodeQueryPanel panel = new EncodeQueryPanel(dialog);
  349. dialog.getContentPane().add(panel);
  350. dialog.pack();
  351. dialog.setVisible(true);
  352. switch(panel.getValue()) {
  353. case JOptionPane.OK_OPTION:
  354. try {
  355. queryData = panel.encode(queryData);
  356. queryDataArea.setText(queryData);
  357. } catch(UnsupportedEncodingException uee) {
  358. report("Encoding problem: " + uee.getMessage());
  359. }
  360. break;
  361. case JOptionPane.CANCEL_OPTION: ;
  362. default: ;
  363. }
  364. }
  365. }
  366.  
  367. /** Listener for Interrupt button. Stops download of
  368. * Web page.
  369. */
  370.  
  371. class InterruptListener implements ActionListener {
  372. public void actionPerformed(ActionEvent event) {
  373. client.setInterrupted(true);
  374. }
  375. }
  376. }

WebClient

效果图:

core servlets & server pages 上面的HttpClient GUI工具的更多相关文章

  1. [Asp.Net Core] Blazor Server Side 扩展用途 - 配合CEF来制作带浏览器核心的客户端软件 (二) 可运行版本

    前言 大概3个星期之前立项, 要做一个 CEF+Blazor+WinForms 三合一到同一个进程的客户端模板. 这个东西在五一的时候做出了原型, 然后慢慢修正, 在5天之前就上传到github了. ...

  2. 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。

    为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracl ...

  3. 10Java Server Pages 隐式对象

    Java Server Pages 隐式对象 JSP隐式对象是Web容器加载的一组类的实例,它不像一般的Java对象那样用“new”去获取实例,而是可以直接在JSP页面使用的对象.JSP提供的隐式对象 ...

  4. 08Java Server Pages 语法

    Java Server Pages 语法 基础语法 注释 <!--   -->可以在客户端通过源代码看到:<%--   --%>在客户端通过查看源代码看不到. <!--浏 ...

  5. 使用.net core基于Razor Pages开发网站一些工作笔记

    本文是在实践工作中遇到的一些问题记录,并给出是如何解决的,.net core已经升级到3.0版本了,其实在项目中很早就已经在使用.net core来开发后台接口了,正好有个网站项目,就使用了Razor ...

  6. 腾讯云-ASP.NET Core+Mysql+Jexus+CDN上云实践

    腾讯云-ASP.NET Core+Mysql+Jexus+CDN上云实践.md 开通腾讯云服务器和Mysql 知识点: ASP.NET Core和 Entity Framework Core的使用 L ...

  7. Windows Server 2012 上安装 dotNET Framework v3.5

    Windows Server 2012不能直接运行dotNET Framework v3.5安装程序进行安装,系统提供通过服务器管理器的添加功能和角色向导进行安装. 安装的前几个步骤再这里略去,在默认 ...

  8. 曹工说mini-dubbo(2)--分析eureka client源码,想办法把我们的服务提供者注册到eureka server(上)

    前言 eureka是spring cloud Netflix技术体系中的重要组件,主要完成服务注册和发现的功能:那现在有个问题,我们自己写的rpc服务,如果为了保证足够的开放性和功能完善性,那肯定要支 ...

  9. CoreCRM 开发实录——Travis-CI 实现 .NET Core 程度在 macOS 上的构建和测试 [无水干货]

    上一篇文章我提到:为了使用"国货",我把 Linux 上的构建和测试委托给了 DaoCloud,而 Travis-CI 不能放着不用啊.还好,这货支持 macOS 系统.所以就把 ...

随机推荐

  1. java的基本数据类型默认值

    这里就举int类型 默认值在类实例化,也就是对象中才有默认值0,或者是静态变量. 1.先看局部变量使用(不行,报错) 2.静态变量 3.类非静态属性

  2. 【TCP/IP详解 卷一:协议】第十章 动态选路协议

    更为详细的RIP博客解析: RIP理论 距离向量算法的简介: RIP协议V-D算法的介绍 10.1 引言 静态选路修改路由表的三种方法 (1)主机设置时,默认的路由表项 (2)ICMP重定向报文(默认 ...

  3. C++ 实验2:函数重载、函数模板、简单类的定义和实现

    1.函数重载编程 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型数据,调用测试. #include <iostream> ...

  4. JavaMai——邮箱验证用户注册

    这篇文章简单的模拟了网上利用邮箱激活用户注册这样的一个功能 1. 呈现给用户的注册界面:(为了简单起见,就剩下两个输入域,邮箱和昵称) <%@ page language="java& ...

  5. TinyXML用法小结2

    参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1.      介绍 Tinyxml的官方网址:http://www.grinn ...

  6. mvn编译

    mvn clean install -pl com:boss -am -DskipTests

  7. angular惰性加载拓展剖析

    最近把一个比较旧的业余项目重新升级了下,将主文件进行了剥离,增加了些惰性加载的配置,将过程中一些零散的知识点做个总结,同时尽量深入原理实现层面. 项目环境: 前端框架:angular2.0.0-bet ...

  8. Java使用Log4记录日志

    我们在系统使用中,为了方便查找问题,因此需要记录操作的日志,而目前比较成熟稳定的程序日志记录方式就是Log4,本人也是菜鸟,然后再学习研究中就记录一下使用方式,以方便今后查阅,同时本文章参考了博客园: ...

  9. 雷林鹏分享:C# 类(Class)

    C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...

  10. English trip -- VC(情景课)9 B Outside chores 室外家务

    Vocabulary focus 核心词汇 cutting the grass 修剪草坪 getting the mail  收到邮件 taking out the trash  把垃圾带出去 wal ...