開發一個支持 SSL 的網頁浏覽器
我們開發一個支持 SSL 的網頁浏覽器作為一個完整的例子
我們創建的浏覽器要能處理任何 URL 如 HTTP
示例代碼
示例代碼
import java
import
import java
import java
import javax
public class QBrowser implements ActionListener
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(
// on close
frame
url = new JTextField(
go = new JButton(
go
JPanel controls = new JPanel(new FlowLayout ());
controls
controls
controls
content = new JEditorPane();
content
// HTML text
// provides support for HTML
content
content
Q s Browser
Copyright (c)
statusLine = new JLabel(
JPanel panel = new JPanel(new BorderLayout (
frame
panel
panel
panel
frame
frame
}
/**
* 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
}
// this is the Thread
public void run () {
try {
String str = url
URL url = new URL(str);
readURL(url);
} catch (IOException ioe) {
statusLine
showException(ioe);
}
}
private void showException(Exception ex) {
StringWriter trace = new StringWriter ();
ex
content
content
+ ex +
}
/**
* The URL class is capable of // and https:// URLs
*/
private void readURL(URL url) throws IOException {
statusLine
URLConnection connection = url
StringBuffer buffer = new StringBuffer();
BufferedReader in=null;
try {
in = new BufferedReader(new InputStreamReader(connection
String line;
while ((line = in
buffer
statusLine
}
} finally {
if(in != null) in
}
String type = connection
if(type == null) type =
statusLine
content
content
statusLine
}
public static void main (String[] args) {
QBrowser browser = new QBrowser();
}
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27643.html