熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

用J2SE 1.4進行Internet安全編程(三)

2022-06-13   來源: Java高級技術 

  開發一個支持 SSL 的網頁浏覽器
  我們開發一個支持 SSL 的網頁浏覽器作為一個完整的例子該浏覽器要做下面的工作
  
   用戶輸入 URL浏覽器能接收它
  
   浏覽器能打開到 URL 指定主機的連接
  
   浏覽器能發送 HTTP 命令
  
   浏覽器會等待 HTTP/HTTPS 服務器的回應
  
   浏覽器能接收 HTML 回應
  
   浏覽器能解析 HTML 並顯示出頁面
  
  我們創建的浏覽器要能處理任何 URL 如 HTTPHTTPSFTP 等注意我使用工具類 lHTMLEditorKit 來解析 HTML它提供了對 HTML 的支持
  
  示例代碼 中展示了這個浏覽器QBrowser的代碼注意 QBrowser 實現了 Runnable 接口我這樣做是因為這個浏覽器沒有提供停止按鈕
  
  示例代碼 QBrowserjava
  
  import javaio*;
  
  import *;
  
  import javaawt*;
  
  import javaawtevent*;
  
  import javaxswing*;
  
  
  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(Qs Browser);
  
   // on close exit the application using Systemexit();
  
   framesetDefaultCloseOperation ();
  
  
   url = new JTextField( );
  
   go = new JButton(Go Get It);
  
   goaddActionListener(this);
  
  
   JPanel controls = new JPanel(new FlowLayout ());
  
   controlsadd(new JLabel(URL:));
  
   controlsadd(url);
  
   controlsadd(go);
  
   content = new JEditorPane();
  
   contentsetEditable(false);
  
   // HTML text Use the kit in the class lHTMLEditorKit which
  
   // provides support for HTML
  
   contentsetContentType(text/html);
  
   contentsetText(

Qs Browser

  Copyright (c) Qusay H Mahmoud

);
  
   statusLine = new JLabel(Initialization Complete);
  
  
   JPanel panel = new JPanel(new BorderLayout ( ));
  
   framesetContentPane(panel);
  
  
   paneladd(controls North);
  
   paneladd(new JScrollPane (content) Center);
  
   paneladd(statusLine South);
  
   framepack();
  
   framesetVisible(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);
  
   threadstart();
  
   }
  
   // this is the Threads run method
  
   public void run () {
  
   try {
  
   String str = urlgetText();
  
   URL url = new URL(str);
  
   readURL(url);
  
   } catch (IOException ioe) {
  
   statusLinesetText(Error: +ioegetMessage());
  
   showException(ioe);
  
   }
  
   }
  
  
   private void showException(Exception ex) {
  
   StringWriter trace = new StringWriter ();
  
   exprintStackTrace (new PrintWriter (trace));
  
   contentsetContentType (text/html);
  
   contentsetText (

+ ex +

   + trace +

);
  
   }
  
  
   /**
  
   * The URL class is capable of // and https:// URLs
  
   */
  
   private void readURL(URL url) throws IOException {
  
   statusLinesetText(Opening + urltoExternalForm());
  
   URLConnection connection = urlopenConnection();
  
   StringBuffer buffer = new StringBuffer();
  
   BufferedReader in=null;
  
   try {
  
   in = new BufferedReader(new InputStreamReader(connectiongetInputStream()));
  
   String line;
  
   while ((line = inreadLine()) != null) {
  
   bufferappend(line)append(\n);
  
   statusLinesetText(Read + bufferlength () + bytes);
  
   }
  
   } finally {
  
   if(in != null) inclose();
  
   }
  
   String type = connectiongetContentType();
  
   if(type == null) type = text/plain;
  
   statusLinesetText(Content type + type);
  
   contentsetContentType(type);
  
   contentsetText(buffertoString());
  
   statusLinesetText(Done);
  
   }
  
  
   public static void main (String[] args) {
  
   QBrowser browser = new QBrowser();
  
   }
  
  }

From:http://tw.wingwit.com/Article/program/Java/gj/201311/27643.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.