服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用j2se 1.4进行internet安全编程(三)


  开发一个支持 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();
  
   }
  
  }

扫描关注微信公众号