网络安全

位置:IT落伍者 >> 网络安全 >> 浏览文章

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


发布日期:2021年03月11日
 
用J2SE 1.4进行Internet安全编程(三)

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

    }

}

               

上一篇:Java实时多任务调度过程中的安全监控设计

下一篇:在 WAS 中使用 Java 安全套接字扩展(图)