您所在的位置: 首页 > 开发 > JAVA > JAVA专区 > J2SE >

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

http://developer.51cto.com  2006-03-22 11:01  Qusay H. Mahmoud 著 边城狂人 译  ccw  我要评论(0)
  • 摘要:这篇文章展示了如何使用 JSSE (SSL 协议的框架和实现) 开发安全的客户端应用程序。这篇文章中的例子展示了将 SSL 整合到 C/S 应用程序是多么简单的事情。这篇文章中讲到一个网页浏览器,QBrowser,可以处理 HTTP 和 HTTPS 请求。
  • 标签:J2SE
示例代码 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();

}

}


共6页: 上一页 [1] [2] [3] 4 [5] [6] 下一页
【内容导航】
J2SE开发应用专栏
JSP开发基础教程
PHP教程 PHP开发基础入门
深入Vista应用程序开发
走向银光 —— 一步一步学Silverlight2
 
 验证码: (点击刷新验证码)   匿名发表
  • Linux C编程实战

  • 作者:童永清
  • 本书系统地介绍了在Linux平台下用C语言进行程序开发的过程,集趣味性、实战性于一体的160多段代码实例,帮助读者快速掌握在Linu..
Copyright©2005-2008 51CTO.COM 版权所有