首页 > 学院 > 开发设计 > 正文

用J2SE 1.4进行Internet安全编程(三)

2019-11-18 12:22:18
字体:
来源:转载
供稿:网友

  开发一个支持 SSL 的网页浏览器
  我们开发一个支持 SSL 的网页浏览器作为一个完整的例子。该浏览器要做下面的工作:
  
  1. 用户输入 URL,浏览器能接收它。
  
  2. 浏览器能打开到 URL 指定主机的连接。
  
  3. 浏览器能发送 HTTP 命令。
  
  4. 浏览器会等待 HTTP/HTTPS 服务器的回应。
  
  5. 浏览器能接收 Html 回应。
  
  6. 浏览器能解析 HTML 并显示出页面。
  
  我们创建的浏览器要能处理任何 URL 如 HTTP、HTTPS、FTP 等。注重我使用工具类 javax.swing.text.html.HTMLEditorKit 来解析 HTML,它提供了对 HTML 3.2 的支持。
  
  示例代码 3 中展示了这个浏览器,QBrowser,的代码。注重 QBrowser 实现了 Runnable 接口。我这样做是因为这个浏览器没有提供“停止”按钮。
  
  示例代码 3:QBrowser.java
  
  import java.io.*;
  
  import java.net.*;
  
  import java.awt.*;
  
  import java.awt.event.*;
  
  import javax.swing.*;
  
  
  public class QBrowser implements ActionListener, Runnable {
  
   PRivate JFrame frame;
  
   private JButton go;
  
   private JEditorPane content;
  
   private JTextField url;
  
   private JLabel statusLine;
  
  
   // default constrUCtor
  
   public QBrowser () {
  
   buildBrowserInterface();
  
   }
  
  
   private void buildBrowserInterface() {
  
   frame = new JFrame("Q's Browser");
  
   // on close, exit the application using System.exit(0);
  
   frame.setDefaultCloSEOperation (3);
  
  
   url = new JTextField("", 25);
  
   go = new JButton("Go Get It");
  
   go.addActionListener(this);
  
  
   JPanel controls = new JPanel(new FlowLayout ());
  
   controls.add(new JLabel("URL:"));
  
   controls.add(url);
  
   controls.add(go);
  
   content = new JEditorPane();
  
   content.setEditable(false);
  
   // HTML text. Use the kit in the class javax.swing.text.html.HTMLEditorKit, which
  
   // provides support for HTML 3.2
  
   content.setContentType("text/html");
  
   content.setText("

Q's Browser

Copyright (c) 2002 Qusay H. Mahmoud

");
  
   statusLine = new JLabel("Initialization Complete");
  
  
   JPanel panel = new JPanel(new BorderLayout (0, 2));
  
   frame.setContentPane(panel);
  
  
   panel.add(controls, "North");
  
   panel.add(new JScrollPane (content), "Center");
  
   panel.add(statusLine, "South");
  
   frame.pack();
  
   frame.setVisible(true);
  
   }
  
  
   /**
  
   * You cannot stop a download with QBrowser
  
   * The thread allows multiple downloads to start
  
   * concurrently in case a download freezes
  
   */
  
   public void actionPerformed (ActionEvent event) {
  
   Thread thread = new Thread(this);
  
   thread.start();
  
   }
  
   // this is the Thread's run method
  
   public void run () {
  
   try {
  
   String str = url.getText();
  
   URL url = new URL(str);
  
   readURL(url);
  
   } catch (IOException ioe) {
  
   statusLine.setText("Error: "+ioe.getMessage());
  
   showException(ioe);
  
   }
  
   }
  
  
   private void showException(Exception ex) {
  
   StringWriter trace = new StringWriter ();
  
   ex.printStackTrace (new PrintWriter (trace));
  
   content.setContentType ("text/html");
  
   content.setText ("

" + ex + "

" + trace + "
");
  
   }
  
  
   /**
  
   * The URL class is capable of handling http:// and https:// URLs
  
   */
  
   private void readURL(URL url) throws IOException {
  
   statusLine.setText("Opening " + url.toExternalForm());
  
   URLConnection connection = url.openConnection();
  
   StringBuffer buffer = new StringBuffer();
  
   BufferedReader in=null;
  
   try {
  
   in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  
   String line;
  
   while ((line = in.readLine()) != null) {
  
   buffer.append(line).append('/n');
  
   statusLine.setText("Read " + buffer.length () + " bytes...");
  
   }
  
   } finally {
  
   if(in != null) in.close();
  
   }
  
   String type = connection.getContentType();
  
   if(type == null) type = "text/plain";
  
   statusLine.setText("Content type " + type);
  
   content.setContentType(type);
  
   content.setText(buffer.toString());
  
   statusLine.setText("Done");
  
   }
  
  
   public static void main (String[] args) {
  
   QBrowser browser = new QBrowser();
  
   }
  
  }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表